#include<stdio.h>
#include <conio.h>
char square[26] = {'o', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
char row[5] = {'1', '2', '3', '4', '5'};
char column[5] = {'1', '2', '3', '4', '5'};
int playingChoice;
int checkWin();
void board();
int main()
{
printf("\n\n");
printf("\t\t\t**** T I C T A C T O E ****\n");
printf("\n\n");
printf("Press 1 for Player Vs Player \nPress 2 for Player Vs Computer\n\n");
scanf("%d", &playingChoice);
//Human player Vs Human player Code
if(playingChoice == 1){
int player = 1, i, choiceRow, choiceColumn;
char mark;
do
{
board();
player = (player % 2) ? 1 : 2;
printf("Player %d, choose row : ", player);
scanf("%d", &choiceRow);
printf("Player %d, choose column : ", player);
scanf("%d", &choiceColumn);
mark = (player == 1) ? 'X' : 'O';
if (choiceRow == 1 && choiceColumn == 1 && square[1] == ' ')
square[1] = mark;
else if (choiceRow == 1 && choiceColumn == 2 && square[2] == ' ')
square[2] = mark;
else if (choiceRow == 1 && choiceColumn == 3 && square[3] == ' ')
square[3] = mark;
else if (choiceRow == 1 && choiceColumn == 4 && square[4] == ' ')
square[4] = mark;
else if (choiceRow == 1 && choiceColumn == 5 && square[5] == ' ')
square[5] = mark;
else if (choiceRow == 2 && choiceColumn == 1 && square[6] == ' ')
square[6] = mark;
else if (choiceRow == 2 && choiceColumn == 2 && square[7] == ' ')
square[7] = mark;
else if (choiceRow == 2 && choiceColumn == 3 && square[8] == ' ')
square[8] = mark;
else if (choiceRow == 2 && choiceColumn == 4 && square[9] == ' ')
square[9] = mark;
else if (choiceRow == 2 && choiceColumn == 5 && square[10] == ' ')
square[10] = mark;
else if (choiceRow == 3 && choiceColumn == 1 && square[11] == ' ')
square[11] = mark;
else if (choiceRow == 3 && choiceColumn == 2 && square[12] == ' ')
square[12] = mark;
else if (choiceRow == 3 && choiceColumn == 3 && square[13] == ' ')
square[13] = mark;
else if (choiceRow == 3 && choiceColumn == 4 && square[14] == ' ')
square[14] = mark;
else if (choiceRow == 3 && choiceColumn == 5 && square[15] == ' ')
square[15] = mark;
else if (choiceRow == 4 && choiceColumn == 1 && square[16] == ' ')
square[16] = mark;
else if (choiceRow == 4 && choiceColumn == 2 && square[17] == ' ')
square[17] = mark;
else if (choiceRow == 4 && choiceColumn == 3 && square[18] == ' ')
square[18] = mark;
else if (choiceRow == 4 && choiceColumn == 4 && square[19] == ' ')
square[19] = mark;
else if (choiceRow == 4 && choiceColumn == 5 && square[20] == ' ')
square[20] = mark;
else if (choiceRow == 5 && choiceColumn == 1 && square[21] == ' ')
square[21] = mark;
else if (choiceRow == 5 && choiceColumn == 2 && square[22] == ' ')
square[22] = mark;
else if (choiceRow == 5 && choiceColumn == 3 && square[23] == ' ')
square[23] = mark;
else if (choiceRow == 5 && choiceColumn == 4 && square[24] == ' ')
square[24] = mark;
else if (choiceRow == 5 && choiceColumn == 5 && square[25] == ' ')
square[25] = mark;
else
{
printf("Invalid move ");
player--;
getch();
}
i = checkWin();
player++;
}while (i == - 1);
board();
if (i == 1)
printf("==>\aPlayer %d win ", --player);
else
printf("==>\aGame draw");
getch();
return 0;
}//end if
}//end int main
void board()
{
system("cls");
printf("\n\n");
printf("\t\t\t**** T I C T A C T O E ****\n");
printf("\n\n");
printf("\n");
printf("\t\t Player 1 place [X] | ");
printf("Player 2 place [O]\n\n\n");
printf("\t\t\t 1 2 3 4 5 \n");
printf("\t\t\t --------------------------------\n");
printf("\t\t\t | | | | | | \n");
printf("\t\t\t1 | %c | %c | %c | %c | %c |\n", square[1], square[2], square[3], square[4], square[5]);
printf("\t\t\t |_____|_____|_____|_____|_____|\n");
printf("\t\t\t | | | | | | \n");
printf("\t\t\t2 | %c | %c | %c | %c | %c |\n", square[6], square[7], square[8], square[9], square[10]);
printf("\t\t\t |_____|_____|_____|_____|_____|\n");
printf("\t\t\t | | | | | | \n");
printf("\t\t\t3 | %c | %c | %c | %c | %c |\n", square[11], square[12], square[13], square[14], square[15]);
printf("\t\t\t |_____|_____|_____|_____|_____|\n");
printf("\t\t\t | | | | | | \n");
printf("\t\t\t4 | %c | %c | %c | %c | %c |\n", square[16], square[17], square[18], square[19], square[20]);
printf("\t\t\t |_____|_____|_____|_____|_____|\n");
printf("\t\t\t | | | | | | \n");
printf("\t\t\t5 | %c | %c | %c | %c | %c |\n", square[21], square[22], square[23], square[24], square[25]);
printf("\t\t\t | | | | | | \n");
printf("\t\t\t --------------------------------\n");
}
int checkWin()
{
if (square[1] == square[2] && square[2] == square[3] && square[3] == square[4] && square[4] == square[5]){
return 1;
}else if (square[6] == square[7] && square[7] == square[8] && square[8] == square[9] && square[9] == square[10]){
return 1;
}else if (square[11] == square[12] && square[12] == square[13] && square[13] == square[14] && square[14] == square[15]){
return 1;
}else if (square[16] == square[17] && square[17] == square[18] && square[18] == square[19] && square[19] == square[20]){
return 1;
}else if (square[21] == square[22] && square[22] == square[23] && square[23] == square[24] && square[24] == square[25]){
return 1;
}else if (square[1] == square[6] && square[6] == square[11] && square[11] == square[16] && square[16] == square[21]){
return 1;
}else if (square[2] == square[7] && square[7] == square[12] && square[12] == square[17] && square[17] == square[22]){
return 1;
}else if (square[3] == square[8] && square[8] == square[13] && square[13] == square[18] && square[18] == square[23]){
return 1;
}else if (square[4] == square[9] && square[9] == square[14] && square[14] == square[19] && square[19] == square[24]){
return 1;
}else if (square[5] == square[10] && square[10] == square[15] && square[15] == square[20] && square[20] == square[25]){
return 1;
}else if (square[1] == square[7] && square[7] == square[13] && square[13] == square[19] && square[19] == square[25]){
return 1;
}else if (square[5] == square[9] && square[9] == square[13] && square[13] == square[17] && square[17] == square[21]){
return 1;
}else if (square[1] != ' ' && square[2] != ' ' && square[3] != ' ' && square[4] != ' ' && square[5] != ' ' && square[6] != ' '
&& square[7] != ' ' && square[8] != ' ' && square[9] != ' ' && square[10] != ' ' && square[11] != ' ' && square[12] != ' '
&& square[13] != ' ' && square[14] != ' ' && square[15] != ' ' && square[16] != ' ' && square[17] != ' ' && square[18] != ' '
&& square[19] != ' ' && square[20] != ' ' && square[21] != ' ' && square[22] != ' ' && square[23] != ' ' && square[24] != ' '
&& square[25] != ' '){
return 0;
}else{
return - 1;
}
}
当玩家输入他想要的行和列时,程序自动显示玩家1为赢家。我认为问题可能是在循环,但我不确定。请帮助我在这个问题上,如果你觉得我的代码幼稚,对不起,因为我是一个初学者,这是我进入编程的第二个月
1条答案
按热度按时间yk9xbfzb1#
WeatherVane指出,您对“five in a row”的检查是不完整的,因为程序没有确保检查是针对五个X还是五个O。因此,以代码中的第一个测试组为例,而不是当前测试
将对测试组中的一个非空白元素进行额外测试。
需要对每个试验组重复检查其中一个元素,以确保其在每个试验块中不为空白。
这样做的变化和故意输入的选择,不导致“五在一排”产生的最终抽奖结果在终端上。
不需要接受这个答案。只是想修饰一下之前的评论,让你看清楚。