分析见我的博客 【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
-
0t.jpg
-
0w.jpg
-
Board.jpg
-
D.jpg
-
Welcome.jpg
-
X.jpg
-
Xt.jpg
-
Xw.jpg
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 图形界面)