Cで作成しているTic-Tac-Toeゲームで問題が発生しています。
ネコゲームのテキストの計算と表示に役立つintcats(ネコゲームはネコゲーム)という変数があります。
私はゲームを結びつける方法を理解するためにほぼ一週間中試みました。あなたたちが私を正しい方向に向けることができたとしても、それは甘いでしょう!
-フレンディン
(PS最初の空のifステートメントは、プレーヤー対コンピューターゲーム用です。ゲームを結び付ける方法がわかれば、その部分を問題なくコーディングできます。ありがとうございました!)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void printTheBoard(char boardPieces[3][3]);
int playerOne(char boardPieces[3][3], int cats);
int playerTwo(char boardPieces[3][3], int cats);
void checkBoard(char boardPieces[3][3], int cats);
int main(void)
{
char boardPieces[3][3] = {'\0'};
int cats = 0; //A tie game in Tic-Tac-Toe is called a Cats Game.
int choice;
printf("Would you like to play against the computer or another player?\n");
printf("Press 1 to play against another player.\n");
printf("Press 2 to play against the computer.\n");
scanf("%d", &choice);
if(choice == 1)
{
for(int i = 0; i < 5; ++i)
{
printTheBoard(boardPieces);
playerOne(boardPieces, cats);
checkBoard(boardPieces, cats);
printTheBoard(boardPieces);
playerTwo(boardPieces, cats);
checkBoard(boardPieces, cats);
}
}
if(choice == 2)
{
}
return 0;
}
void printTheBoard(char boardPieces[3][3])
{
printf("\n %c | %c | %c\n", boardPieces[0][0], boardPieces[0][1], boardPieces[0][2]);
printf("---|---|---\n");
printf(" %c | %c | %c\n", boardPieces[1][0], boardPieces[1][1], boardPieces[1][2]);
printf("---|---|---\n");
printf(" %c | %c | %c\n\n", boardPieces[2][0], boardPieces[2][1], boardPieces[2][2]);
}
int playerOne(char boardPieces[3][3], int cats)
{
int choice;
printf("It is your turn. You are player O. Please enter in the space you would like to take. The first space is 1, continuing left to right, top to bottom\n");
scanf("%d", &choice);
if(choice == 1)
{
boardPieces[0][0] = 'O';
++cats;
return cats;
}
else if(choice == 2)
{
boardPieces[0][1] = 'O';
++cats;
return cats;
}
else if(choice == 3)
{
boardPieces[0][2] = 'O';
++cats;
return cats;
}
else if(choice == 4)
{
boardPieces[1][0] = 'O';
++cats;
return cats;
}
else if(choice == 5)
{
boardPieces[1][1] = 'O';
++cats;
return cats;
}
else if(choice == 6)
{
boardPieces[1][2] = 'O';
++cats;
return cats;
}
else if(choice == 7)
{
boardPieces[2][0] = 'O';
++cats;
return cats;
}
else if(choice == 8)
{
boardPieces[2][1] = 'O';
++cats;
return cats;
}
else if(choice == 9)
{
boardPieces[2][2] = 'O';
++cats;
return cats;
}
}
int playerTwo(char boardPieces[3][3], int cats)
{
int choice;
printf("It is your turn. You are player X. Please enter in the space you would like to take. The first space is 1, continuing left to right, top to bottom\n");
scanf("%d", &choice);
if(choice == 1)
{
boardPieces[0][0] = 'X';
++cats;
return cats;
}
else if(choice == 2)
{
boardPieces[0][1] = 'X';
++cats;
return cats;
}
else if(choice == 3)
{
boardPieces[0][2] = 'X';
++cats;
return cats;
}
else if(choice == 4)
{
boardPieces[1][0] = 'X';
++cats;
return cats;
}
else if(choice == 5)
{
boardPieces[1][1] = 'X';
++cats;
return cats;
}
else if(choice == 6)
{
boardPieces[1][2] = 'X';
++cats;
return cats;
}
else if(choice == 7)
{
boardPieces[2][0] = 'X';
++cats;
return cats;
}
else if(choice == 8)
{
boardPieces[2][1] = 'X';
++cats;
return cats;
}
else if(choice == 9)
{
boardPieces[2][2] = 'X';
++cats;
return cats;
}
}
void checkBoard(char boardPieces[3][3], int cats)
{
if(boardPieces[0][0] == 'O')
{
if(boardPieces[0][1] == 'O')
{
if(boardPieces[0][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
else if(boardPieces[1][1] == 'O')
{
if(boardPieces[2][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
else if(boardPieces[1][0] == 'O')
{
if(boardPieces[2][0] == 'O')
{
printf("Player O has won the game!\n");
}
}
}
else if(boardPieces[0][2] == 'O')
{
if(boardPieces[1][1] == 'O')
{
if(boardPieces[2][0] == 'O')
{
printf("Player O has won the game!\n");
}
}
else if(boardPieces[1][2] == 'O')
{
if(boardPieces[2][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
}
else if(boardPieces[1][1] == 'O')
{
if(boardPieces[0][1] == 'O')
{
if(boardPieces[2][1] == 'O')
{
printf("Player O has won the game!\n");
}
}
else if(boardPieces[1][0] == 'O')
{
if(boardPieces[1][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
}
else if(boardPieces[2][0] == 'O')
{
if(boardPieces[2][1] == 'O')
{
if(boardPieces[2][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
}
if(boardPieces[0][0] == 'X')
{
if(boardPieces[0][1] == 'X')
{
if(boardPieces[0][2] == 'X')
{
printf("Player X has won the game!\n");
}
}
else if(boardPieces[1][1] == 'X')
{
if(boardPieces[2][2] == 'X')
{
printf("Player X has won the game!\n");
}
}
else if(boardPieces[1][0] == 'X')
{
if(boardPieces[2][0] == 'X')
{
printf("Player X has won the game!\n");
}
}
}
else if(boardPieces[0][2] == 'X')
{
if(boardPieces[1][1] == 'X')
{
if(boardPieces[2][0] == 'X')
{
printf("Player X has won the game!\n");
}
}
else if(boardPieces[1][2] == 'X')
{
if(boardPieces[2][2] == 'X')
{
printf("Player X has won the game!\n");
}
}
}
else if(boardPieces[1][1] == 'X')
{
if(boardPieces[0][1] == 'X')
{
if(boardPieces[2][1] == 'X')
{
printf("Player X has won the game!\n");
}
}
else if(boardPieces[1][0] == 'X')
{
if(boardPieces[1][2] == 'X')
{
printf("Player X has won the game!\n");
}
}
}
else if(boardPieces[2][0] == 'X')
{
if(boardPieces[2][1] == 'X')
{
if(boardPieces[2][2] == 'X')
{
printf("Player O has won the game!\n");
}
}
}
else if(cats == 9)
{
printf("There was a tie game!\n");
}
}