程式碼:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char map[3][3] = {'\0'};
void initializeboard()
{
int number = 1;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
map[i][j] = ('0'+number++);
}
}
void printborad()
{
printf("-------------\n");
printf("| %c | %c | %c |\n",map[0][0],map[0][1],map[0][2]);
printf("----+---+---\n");
printf("| %c | %c | %c |\n", map[1][0], map[1][1], map[1][2]);
printf("----+---+----\n");
printf("| %c | %c | %c |\n", map[2][0], map[2][1], map[2][2]);
printf("-------------\n");
}
void playerturn()
{
int cmd,correct_input=0;
char mark='o';
while (1)
{
printf("請選擇編號: ");
scanf_s("%d", &cmd);
if (cmd == 1 && map[0][0] == '1')
map[0][0] = 'o', correct_input = 1;
else if (cmd == 2 && map[0][1] == '2')
map[0][1] = 'o', correct_input = 1;
else if (cmd == 3 && map[0][2] == '3')
map[0][2] = 'o', correct_input = 1;
else if (cmd == 4 && map[1][0] == '4')
map[1][0] = 'o', correct_input = 1;
else if (cmd == 5 && map[1][1] == '5')
map[1][1] = 'o', correct_input = 1;
else if (cmd == 6 && map[1][2] == '6')
map[1][2] = 'o', correct_input = 1;
else if (cmd == 7 && map[2][0] == '7')
map[2][0] = 'o', correct_input = 1;
else if (cmd == 8 && map[2][1] == '8')
map[2][1] = 'o', correct_input = 1;
else if (cmd == 9 && map[2][2] == '9')
map[2][2] = 'o', correct_input = 1;
else
printf("錯誤輸入,請再輸入一次!\n");
if (correct_input)
break;
}
}
void computerturn()
{
int x = rand() % 3, y = rand() % 3,i=0;
while (map[x][y] == 'o' || map[x][y] == 'x')
{
x = rand() % 3, y = rand() % 3;
i++;
if (i >= 9)
return;
}
map[x][y] = 'x';
}
int winorlose()
{
// o win
if (map[0][0] == 'o'&&map[0][1] == 'o'&&map[0][2] == 'o') return 1; //橫向
if (map[1][0] == 'o'&&map[1][1] == 'o'&&map[1][2] == 'o') return 1;
if (map[2][0] == 'o'&&map[2][1] == 'o'&&map[2][2] == 'o') return 1;
if (map[0][0] == 'o'&&map[1][0] == 'o'&&map[2][0] == 'o') return 1; //縱向
if (map[0][1] == 'o'&&map[1][1] == 'o'&&map[2][1] == 'o') return 1;
if (map[0][2] == 'o'&&map[1][2] == 'o'&&map[2][2] == 'o') return 1;
if (map[0][0] == 'o'&&map[1][1] == 'o'&&map[2][2] == 'o') return 1; //斜向
if (map[0][2] == 'o'&&map[1][1] == 'o'&&map[2][0] == 'o') return 1;
// x win
if (map[0][0] == 'x'&&map[0][1] == 'x'&&map[0][2] == 'x') return 2;
if (map[1][0] == 'x'&&map[1][1] == 'x'&&map[1][2] == 'x') return 2;
if (map[2][0] == 'x'&&map[2][1] == 'x'&&map[2][2] == 'x') return 2;
if (map[0][0] == 'x'&&map[1][0] == 'x'&&map[2][0] == 'x') return 2; //縱向
if (map[0][1] == 'x'&&map[1][1] == 'x'&&map[2][1] == 'x') return 2;
if (map[0][2] == 'x'&&map[1][2] == 'x'&&map[2][2] == 'x') return 2;
if (map[0][0] == 'x'&&map[1][1] == 'x'&&map[2][2] == 'x') return 2; //斜向
if (map[0][2] == 'x'&&map[1][1] == 'x'&&map[2][0] == 'x') return 2;
int record = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (map[i][j] == 'o' || map[i][j] == 'x')
record++;
}
}
if (record == 9) return 3;
return 0;
}
int main()
{
srand(time(NULL));
printf("\n\n\n");
printf("\t\t\t~~歡迎來玩 井字遊戲~~\n\n");
printf("\t\t\t <規則>\n");
printf("\t\t\t1.玩家為先手,與電腦對決\n");
printf("\t\t\t2.先連成一線者獲勝\n\n\n");
system("pause");
system("cls");
initializeboard();
int result = 0;
while (1)
{
system("cls");
printborad();
playerturn();
if ((result = winorlose()) != 0)
break;
computerturn();
if ((result = winorlose()) != 0)
break;
}
system("cls");
printborad();
if (result == 1)
printf("你贏了~\n");
else if (result == 2)
printf("你輸了~\n");
else if (result == 3)
printf("平手~\n");
system("pause");
return 0;
}
沒有留言:
張貼留言