gpt4 book ai didi

c++ - TicTacToe 游戏,用户再次玩游戏后棋盘未清除

转载 作者:行者123 更新时间:2023-11-28 04:11:05 25 4
gpt4 key购买 nike

<分区>

我为作业制作了一个简单的滴答游戏。一切正常,除非用户想要再次玩游戏,游戏从前一个棋盘正常开始,我尝试了不同的方法来清除棋盘,但似乎都没有用。我试图在 again 函数中重新输入字符,但它们没有保留下来。谢谢。

#include <iostream>
using namespace std;

//Board variables
char s1('1');
char s2('2');
char s3('3');
char s4('4');
char s5('5');
char s6('6');
char s7('7');
char s8('8');
char s9('9');

int checkifwin();
int again = 0;
void createboard();
void playagain();
void endpro();

void createboard()
{
//This function draws the board

system("clear");

cout << "-----------------" << endl;
cout << " TIC TAC TOE v0.2 " << endl;
cout << "-----------------" << endl;
cout << " P1(X) - P2(O)" << endl;
cout << "-----------------" << endl;
cout << "Choices are numbered" << endl;
cout << "1 to 9, from left to right" << endl;
cout << "Press a # to place your marker" << endl;

cout << "____________________" << endl;
cout << "| || || |" << endl;
cout << "| " << s1 << " || " << s2 << " || " << s3 << " |" << endl;
cout << "|_____||_____||_____|" << endl;
cout << "| || || |" << endl;
cout << "| " << s4 << " || " << s5 << " || " << s6 << " |" << endl;
cout << "|_____||_____||_____|" << endl;
cout << "| || || |" << endl;
cout << "| " << s7 << " || " << s8 << " || " << s9 << " |" << endl;
cout << "|_____||_____||_____|" << endl;

}

int main()
{
int player = 1;
int i;
int choice;
char mark = ' ';

do
{

createboard();

cout << "Player # " << player << ", please enter a number: ";
cin >> choice;

if(player == 1)
{
mark = 'x';
player++;
}
else if(player == 2)
{
mark = 'o';
player--;
}

if (choice == 1 && s1 == '1')
s1 = mark;
else if (choice == 2 && s2 == '2')
s2 = mark;
else if (choice == 3 && s3 == '3')
s3 = mark;
else if (choice == 4 && s4 == '4')
s4 = mark;
else if (choice == 5 && s5 == '5')
s5 = mark;
else if (choice == 6 && s6 == '6')
s6 = mark;
else if (choice == 7 && s7 == '7')
s7 = mark;
else if (choice == 8 && s8 == '8')
s8 = mark;
else if (choice == 9 && s9 == '9')
s9 = mark;
else
{
cout << "Not a valid move, try again please. ";
cin.ignore();
cin.get();
}

i = checkifwin();

} while (i == -1);

if (i == 1)
if(player = 2)
cout << "Player #1 is the winner!" << endl;
else if(player = 1)
cout << "Player #2 is the winner!" << endl;
else if (i == 0)
cout << "So close! It's a tie!" << endl;

playagain();
}

int checkifwin()
{
//This function returns the game status, 1 is for game over with results,
//-1 is for game in progress, and 0 is for gameover and no result(tie)

if (s1 == s2 && s2 == s3)
return 1;

else if (s1 == s4 && s4 == s7)
return 1;

else if (s1 == s5 && s5 == s9)
return 1;

else if (s2 == s5 && s5 == s8)
return 1;

else if (s3 == s6 && s6 == s9)
return 1;

else if (s3 == s5 && s5 == s7)
return 1;

else if (s4 == s5 && s5 == s6)
return 1;

else if (s7 == s8 && s8 == s9)
return 1;

else if (s1 != '1' && s2 != '2' && s3 != '3' && s4 != '4' && s5 != '5' && s6 != '6' && s7 != '7' && s8 != '8' && s9 != '9')
return 0;

else
return -1;
}

void playagain()
{
//Asks the player if they
//want to play again
cout << "Would you like to play again?" << endl;
cout << "1 = Yes, 2 = No." << endl;
cin >> again;

if(again == 1)
{
s1 == '1';
s2 == '2';
s3 == '3';
s4 == '4';
s5 == '5';
s6 == '6';
s7 == '7';
s8 == '8';
s9 == '9';

main();
}
else if(again == 2)
{
cout << "Press ENTER to confirm..." << endl;
endpro();
}
}

void endpro()
{
//Ends the program
return;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com