无聊时写了一个井字棋小游戏,大家可以玩玩
源码
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#define ROW 4
#define COL 4
//#define
using namespace std;
const double Loading_num[11][2] = {{0, 0}, {9.81, 12.12}, {17.63, 22.34}, {29.25, 33.36}, {40.47, 41.78}, {46.49, 51.60}, {63.11, 64.92}, {73.3, 75.54}, {84.35, 88.76}, {90.47, 94.78}, {100,100}};
const int INF = 1 << 30;
const int N = 3;
int n = 3, cnt, a[ROW][COL], cnt_player, cnt_computer, cnt_player1, cnt_player2, step, f1, f2;
inline void Delay (int time) //sleep
{
clock_t now = clock();
while (clock() - now < time);
}
inline void Loading()
{
// clock_t now = clock();
for (int i = 0; i <= 10; ++i)
{
puts ("");
puts ("Loading...");
puts ("----------");
for (int j = 1; j <= i; ++j) putchar ('|');
puts ("");
puts ("----------");
putchar ('%');
srand (time (0));
double rand_num = rand();
double mod = Loading_num[i][1] - Loading_num[i][0] + 1;
double val = (rand_num - int(rand_num / mod) * mod) + Loading_num[i][0];
printf ("%.2f", val);
puts ("");
Delay (0.5 * 1000);
system ("cls");
}
Delay (0.625 * 1000);
system ("cls");
}
inline void init()
{
f1 = f2 = 0;
step = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
a[i][j] = INF;
// system ("pause");
}
inline bool isLegitimate (int x, int y)
{
return a[x][y] == INF;
}
inline void PutChar(int x)
{
switch (x)
{
case 0: putchar ('X'); break;
case 1: putchar ('O'); break;
default: putchar (' '); break; //INF
}
}
inline void print() //print_ans
{
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
PutChar (a[i][j]);
if (j < n) putchar ('|');
}
puts ("");
if (i < n) puts ("-----");
}
}
inline void read_1()
{
char op = getch(); //getch()
if (op == ' ') //exit
{
puts ("\n");
puts ("Answer: \n");
printf ("%d : %d\n\n", cnt_player, cnt_computer);
if (cnt_player > cnt_computer)
{
puts ("Player Wins!!!");
}
else if (cnt_player < cnt_computer)
{
puts ("Computer Wins!!!");
}
else
puts ("Tie!!!");
puts ("");
f1 = 1;
return ;
// exit (0);
}
system ("cls");
puts ("\n");
printf ("Round %d\n", ++cnt);
puts ("");
printf ("%d : %d\n", cnt_player, cnt_computer);
puts ("\n");
Delay (1.5 * 1000);
system ("cls");
}
inline void MoveChess()
{
int x, y;
while (a[x][y] != INF)
x = rand() & 3, y = rand() & 3;
a[x][y] = 1;
}
inline bool Check_1()
{
for (int i = 1;i <= n; ++i) //row
{
if ((a[i][1] == a[i][2] && a[i][2] == a[i][3]) && (a[i][2] != INF))
{
if (!a[i][2])
puts ("Player Wins!!!"), ++cnt_player;
else
puts ("Computer Wins!!!"), ++cnt_computer;
return 1;
}
}
for (int i = 1;i <= n; ++i) //column
{
if ((a[1][i] == a[2][i] && a[2][i] == a[3][i]) && (a[2][i] != INF))
{
if (!a[2][i])
puts ("Player Wins!!!"), ++cnt_player;
else
puts ("Computer Wins!!!"), ++cnt_computer;
return 1;
}
}
// diagonal
if ((a[1][1] == a[2][2] && a[2][2] == a[3][3]) && (a[2][2] != INF))
{
if (!a[2][2])
puts ("Player Wins!!!"), ++cnt_player;
else
puts ("Computer Wins!!!"), ++cnt_computer;
return 1;
}
if ((a[1][3] == a[2][2] && a[2][2] == a[3][1]) && (a[2][2] != INF))
{
if(!a[2][2])
puts ("Player Wins!!!"), ++cnt_player;
else
puts ("Computer Wins!!!"), ++cnt_computer;
return 1;
}
return 0;
}
inline bool isTie_1()
{
if (step == n * n && !Check_1())
{
puts ("Tie!!!");
return 1;
}
return 0;
}
inline void Play()
{
for (int i = 3;i; --i)
{
puts ("---------------");
puts (" Start : Enter ");
puts (" Exit : Space ");
puts ("---------------");
printf ("三秒后跳转页面");
for (int j = 1; j <= i; ++j)
putchar ('.');
Delay (1000);
system ("cls");
}
Delay (185);
print ();
puts ("");
while (1)
{
init ();
if (cnt)
read_1 ();
else
++cnt;
if (f1)
{
f1 = 0;
puts ("Please press any key to return...");
getch ();
return ;
}
if (cnt > 1)
print();
for (int i = 0; ; ++i) //MoveChess
{
int x, y;
scanf ("%d %d", &x, &y);
while (!isLegitimate(x,y))
{
puts ("");
puts ("Error!");
puts ("");
scanf ("%d %d", &x, &y);
}
a[x][y] = 0;
++step;
system ("cls");
print ();
puts ("");
if (Check_1())
break;
if (isTie_1())
break;
MoveChess ();
++step;
system ("cls");
print ();
puts ("");
if (Check_1())
break;
if (isTie_1())
break;
}
}
}
inline void read_2()
{
char op = getch(); //getch()
if (op == ' ') //exit
{
puts ("\n");
puts ("Answer: \n");
printf ("%d : %d\n\n", cnt_player1, cnt_player2);
if (cnt_player1 > cnt_player2)
{
puts ("Player1 Wins!!!");
}
else if (cnt_player1 < cnt_player2)
{
puts ("Player2 Wins!!!");
}
else
puts ("Tie!!!");
puts ("");
f2 = 1;
return ;
// exit (0);
}
puts ("\n");
printf ("Round %d\n", ++cnt);
puts ("");
printf ("%d : %d\n", cnt_player1, cnt_player2);
Delay (1.5 * 1000);
system ("cls");
}
inline bool Check_2()
{
for (int i = 1; i <= n; ++i) //row
{
if ((a[i][1] == a[i][2] && a[i][2] == a[i][3]) && (a[i][2] != INF))
{
if (!a[i][2])
puts ("Player1 Wins!!!"), ++cnt_player1;
else
puts ("Player2 Wins!!!"), ++cnt_player2;
return 1;
}
}
for (int i = 1; i <= n; ++i) //column
{
if ((a[1][i] == a[2][i] && a[2][i] == a[3][i]) && (a[2][i] != INF))
{
if (!a[2][i])
puts ("Player1 Wins!!!"), ++cnt_player1;
else
puts ("Player2 Wins!!!"), ++cnt_player2;
return 1;
}
}
// diagonal
if ((a[1][1] == a[2][2] && a[2][2] == a[3][3]) && (a[2][2] != INF))
{
if (!a[2][2])
puts ("Player1 Wins!!!"), ++cnt_player1;
else
puts ("Player2 Wins!!!"), ++cnt_player2;
return 1;
}
if ((a[1][3] == a[2][2] && a[2][2] == a[3][1]) && (a[2][2] != INF))
{
if (!a[2][2])
puts ("Player1 Wins!!!"), ++cnt_player1;
else
puts ("Player2 Wins!!!"), ++cnt_player2;
return 1;
}
return 0;
}
inline bool isTie_2()
{
if (step == n * n && !Check_2())
{
puts ("Tie!!!");
return 1;
}
return 0;
}
inline void Two_Players()
{
for (int i = 3; i ; --i)
{
puts ("---------------");
puts (" Start : Enter ");
puts (" Exit : Space ");
puts ("---------------");
printf ("三秒后跳转页面");
for (int j = 1;j <= i; ++j)
putchar ('.');
Delay (1000);
system ("cls");
}
Delay (185);
print ();
puts ("");
while (1)
{
init ();
if (cnt)
read_2 ();
else
++cnt;
if (f2)
{
f2 = 0;
puts ("Please press any key to return...");
getch ();
return ;
}
if (cnt > 1)
print ();
for (int i = 0;; ++i) //MoveChess
{
int x, y, c;
c = i % 2;
scanf ("%d %d", &x, &y);
while (!isLegitimate (x, y))
{
puts ("");
puts ("Error!");
puts ("");
scanf ("%d %d", &x, &y);
}
a[x][y] = c;
++step;
system ("cls");
print ();
puts ("");
if (Check_2())
break;
if (isTie_2())
break;
}
}
}
signed main()
{
Loading();
// Delay (150);
while (1)
{
system ("cls");
puts ("井字棋小游戏");
puts ("\n");
puts ("Play: 1");
puts ("Two Players: 2");
puts ("Description: 3");
puts ("");
puts ("Please press Space to exit!!!");
char op = getch();
if (op == ' ')
exit (0);
system ("cls");
init ();
switch (op)
{
case '1':
{
cnt = 0;
cnt_player = cnt_computer = 0;
Play();
break;
}
case '2':
{
cnt = 0;
cnt_player1 = cnt_player2 = 0;
Two_Players ();
break;
}
case '3':
{
puts ("\n");
puts ("说明:");
puts ("");
puts ("井字棋游戏:井字棋是一种益智小游戏。图纸为3 x 3的方格,选手1和选手2分别画X和画O,如果X或O连成三个,就代表那一方赢");
puts ("");
puts ("Please press any key to return...");
getch ();
break;
}
}
}
return 0;
}
制作不易,赶紧点个关注收藏一下吧