【C++ 程序】 井字棋游戏(人 VS 人)(EasyX 图形界面)

分析见我的博客 【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)(EasyX 图形界面)
提一句,不知为什么我的电脑将其认定为病毒。自己编的中是第4次遇到这种情况了。
注意要下载 Easy-X,其中包含库graphics.h

程序

//This program is a simple tic-tac-toe game.

#include <iostream>
#include <string>
#include <cstddef>
#include <stdexcept>
#include <Windows.h>
#include <graphics.h>
#include <conio.h>
using namespace std;

IMAGE imageX, image0, imageXt, image0t, imageXw, image0w, imageD;

char point_now[3][3] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
char player = 'X';
unsigned step_count = 0;

int win_lose(char point[3][3], int n)
{
	if (point[0][0] == point[0][1] && point[0][1] == point[0][2] && point[0][0] == 'X') return 1; // X wins
	if (point[1][0] == point[1][1] && point[1][1] == point[1][2] && point[1][0] == 'X') return 1; // X wins
	if (point[2][0] == point[2][1] && point[2][1] == point[2][2] && point[2][0] == 'X') return 1; // X wins
	if (point[0][0] == point[1][0] && point[1][0] == point[2][0] && point[0][0] == 'X') return 1; // X wins
	if (point[0][1] == point[1][1] && point[1][1] == point[2][1] && point[0][1] == 'X') return 1; // X wins
	if (point[0][2] == point[1][2] && point[1][2] == point[2][2] && point[0][2] == 'X') return 1; // X wins
	if (point[0][0] == point[1][1] && point[1][1] == point[2][2] && point[1][1] == 'X') return 1; // X wins
	if (point[0][2] == point[1][1] && point[1][1] == point[2][0] && point[1][1] == 'X') return 1; // X wins

	if (point[0][0] == point[0][1] && point[0][1] == point[0][2] && point[0][0] == '0') return 2; // 0 wins
	if (point[1][0] == point[1][1] && point[1][1] == point[1][2] && point[1][0] == '0') return 2; // 0 wins
	if (point[2][0] == point[2][1] && point[2][1] == point[2][2] && point[2][0] == '0') return 2; // 0 wins
	if (point[0][0] == point[1][0] && point[1][0] == point[2][0] && point[0][0] == '0') return 2; // 0 wins
	if (point[0][1] == point[1][1] && point[1][1] == point[2][1] && point[0][1] == '0') return 2; // 0 wins
	if (point[0][2] == point[1][2] && point[1][2] == point[2][2] && point[0][2] == '0') return 2; // 0 wins
	if (point[0][0] == point[1][1] && point[1][1] == point[2][2] && point[1][1] == '0') return 2; // 0 wins
	if (point[0][2] == point[1][1] && point[1][1] == point[2][0] && point[1][1] == '0') return 2; // 0 wins

	if (n == 9) return 3; // end up in a draw
	else return 0; // unfinished
}

void game_player_change(char& player)
{
	if (player == 'X')
		player = '0'; // X -> 0
	else player = 'X';// 0 -> X
}

void init()
{
	initgraph(1200, 800);
	loadimage(0, _T("Welcome.jpg"), 1200, 800);//std::cout << "This program is a simple tic-tac-toe game.\nProgrammer:Teddy van Jerry\n" << endl;
	Sleep(3000);
	loadimage(0, _T("Board.jpg"), 1200, 800);// the board
}

string man_input()
{
	int Check = 0;
	string ret = "00";
	while (Check == 0)
	{
		if (MouseHit())
		{
			MOUSEMSG mouse = GetMouseMsg();
			if (mouse.uMsg == WM_LBUTTONDOWN)
			{
				/**/ if (mouse.y > 150 && mouse.y < 350) ret[0] = 'A';
				else if (mouse.y > 350 && mouse.y < 550) ret[0] = 'B';
				else if (mouse.y > 550 && mouse.y < 750) ret[0] = 'C';
				/**/ if (mouse.x > 100 && mouse.x < 300) ret[1] = '1';
				else if (mouse.x > 300 && mouse.x < 500) ret[1] = '2';
				else if (mouse.x > 500 && mouse.x < 700) ret[1] = '3';
			}
		}
		if (ret[0] != '0' && ret[1] != '0')
			if (point_now[ret[0] - 65][ret[1] - 49] == ' ')
				Check = 1; // indicate it's a legal click
	}
	return ret;
}

void chess(string str,char c)
{
	if (c == 'X')
	{
		loadimage(&imageX, _T("X.jpg"), 180, 180);
		/**/ if (str == "A1") putimage(110, 160, &imageX);
		else if (str == "A2") putimage(314, 160, &imageX);
		else if (str == "A3") putimage(518, 160, &imageX);
		else if (str == "B1") putimage(110, 360, &imageX);
		else if (str == "B2") putimage(314, 360, &imageX);
		else if (str == "B3") putimage(518, 360, &imageX);
		else if (str == "C1") putimage(110, 560, &imageX);
		else if (str == "C2") putimage(314, 560, &imageX);
		else if (str == "C3") putimage(518, 560, &imageX);
	}
	else
	{
		loadimage(&image0, _T("0.jpg"), 180, 180);
		/**/ if (str == "A1") putimage(110, 160, &image0);
		else if (str == "A2") putimage(314, 160, &image0);
		else if (str == "A3") putimage(518, 160, &image0);
		else if (str == "B1") putimage(110, 360, &image0);
		else if (str == "B2") putimage(314, 360, &image0);
		else if (str == "B3") putimage(518, 360, &image0);
		else if (str == "C1") putimage(110, 560, &image0);
		else if (str == "C2") putimage(314, 560, &image0);
		else if (str == "C3") putimage(518, 560, &image0);
	}
}

void tip(char c)
{
	if (c == 'X')
	{
		loadimage(&imageXt, _T("Xt.jpg"), 100, 100);
		putimage(1040, 190, &imageXt);
	}
	if (c == '0')
	{
		loadimage(&imageXt, _T("0t.jpg"), 100, 100);
		putimage(1040, 190, &image0t);
	}
}

int main()
{
	init();
	while (win_lose(point_now, step_count) == 0)
	{
		string location;
		unsigned location_letter = 0; // A/B/C
		unsigned location_number = 0; // 1/2/3
		tip(player);
		location = man_input();
		chess(location,player); // print the chess
		switch (location[0])
		{
		case 'A':
			location_letter = 0;
			break;
		case 'B':
			location_letter = 1;
			break;
		case 'C':
			location_letter = 2;
			break;
		default:
			break;
		}

		switch (location[1])
		{
		case '1':
			location_number = 0;
			break;
		case '2':
			location_number = 1;
			break;
		case '3':
			location_number = 2;
			break;
		default:
			break;
		}
		point_now[location_letter][location_number] = player;

		game_player_change(player); // change the player
		++step_count; // count one more time
		std::cout << endl;
	}

	int final_result = win_lose(point_now, step_count);
	switch (final_result)
	{
	case 1:
		loadimage(&imageXw, _T("Xw.jpg"), 390, 500);
		putimage(780, 100, &imageXw);
		break;
	case 2:
		loadimage(&image0w, _T("0w.jpg"), 390, 500);
		putimage(780, 100, &image0w);
		break;
	case 3:
		loadimage(&imageD, _T("D.jpg"), 390, 500);
		putimage(780, 100, &imageD);
		break;
	}
	getchar();
	//std::cout << "\nALL RIGHTS RESERVED (c) 2020 Teddy van Jerry" << endl;
	return 0;
}

//Copyright :2020 Teddy van Jerry

素材

  • 0.jpg
    0

  • 0t.jpg
    0t

  • 0w.jpg0w

  • Board.jpgBoard

  • D.jpgD

  • Welcome.jpgWelcome

  • X.jpg
    X

  • Xt.jpg
    Xt

  • Xw.jpg
    Xw


ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。


See also

【C++ 程序】 井字棋游戏(人 VS 人)
【C++ 程序】 井字棋游戏(人 VS Lv1电脑)
【C++ 程序】 井字棋游戏(人 VS Lv2电脑)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)
【C++ 程序】 五子棋游戏(人 VS 人)
【C++ 程序】 五子棋游戏(人 VS Lv1电脑)(思路及框架,内容待填充)
【C++ 程序】 随机数
【C++ 程序】 移动迷宫游戏
【C++ 程序】 贪吃蛇游戏
【C++ 程序】 数字推盘游戏(15-puzzle)
【C++ 程序】 2048游戏
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)(EasyX 图形界面)
【C++ 程序】 2048游戏(EasyX 图形界面)
【C++ 程序】 贪吃蛇游戏(EasyX 图形界面)

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
C++井字棋游戏代码可以使用EasyX图形库进行实现,下面是一个简单的示例代码: ``` #include <graphics.h> #include <conio.h> const int kBoardSize = 3; // 棋盘大小 // 绘制棋盘 void DrawBoard() { cleardevice(); // 清空屏幕 int width = getwidth() / kBoardSize; int height = getheight() / kBoardSize; // 绘制横线 for (int i = 1; i < kBoardSize; i++) { line(0, i * height, getwidth(), i * height); } // 绘制竖线 for (int i = 1; i < kBoardSize; i++) { line(i * width, 0, i * width, getheight()); } } // 下棋 void PlayChess(int row, int col, bool is_black) { int x = col * getwidth() / kBoardSize + getwidth() / kBoardSize / 2; int y = row * getheight() / kBoardSize + getheight() / kBoardSize / 2; if (is_black) { setcolor(BLACK); circle(x, y, getwidth() / kBoardSize / 2 - 10); } else { setcolor(WHITE); line(x - getwidth() / kBoardSize / 2 + 10, y - getheight() / kBoardSize / 2 + 10, x + getwidth() / kBoardSize / 2 - 10, y + getheight() / kBoardSize / 2 - 10); line(x - getwidth() / kBoardSize / 2 + 10, y + getheight() / kBoardSize / 2 - 10, x + getwidth() / kBoardSize / 2 - 10, y - getheight() / kBoardSize / 2 + 10); } } int main() { initgraph(600, 600); DrawBoard(); bool is_black = true; // 黑棋先手 while (true) { if (_kbhit()) { // 如果有按键按下 char ch = _getch(); if (ch == 27) break; // 如果是Esc键,退出游戏 int row = -1; int col = -1; bool can_put = false; switch (ch) { case 'q': row = 0; col = 0; can_put = true; break; case 'w': row = 0; col = 1; can_put = true; break; case 'e': row = 0; col = 2; can_put = true; break; case 'a': row = 1; col = 0; can_put = true; break; case 's': row = 1; col = 1; can_put = true; break; case 'd': row = 1; col = 2; can_put = true; break; case 'z': row = 2; col = 0; can_put = true; break; case 'x': row = 2; col = 1; can_put = true; break; case 'c': row = 2; col = 2; can_put = true; break; } if (can_put) { PlayChess(row, col, is_black); is_black = !is_black; } } } closegraph(); return 0; } ``` 此示例中,通过`initgraph`函数初始化图形界面,然后在`DrawBoard`函数中绘制棋盘,`PlayChess`函数用于在棋盘上下棋。在`main`函数中,通过`_kbhit`函数检测键盘是否有按键按下,然后根据按键的不同来确定落子的位置,通过`PlayChess`函数在棋盘上下棋,并通过`is_black`变量来控制黑白棋的交替。当按下Esc键时,游戏退出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值