gpt4 book ai didi

c++ - 在 C++ 中重新启动程序

转载 作者:搜寻专家 更新时间:2023-10-31 02:08:27 24 4
gpt4 key购买 nike

看看这段代码,这是一个井字游戏游戏结束后,程序会询问用户是否要再次玩,如果是,则该功能会再次运行。但是,数组值保持更新,因为它已在全局声明。该数组必须全局声明,因为它在多个函数中使用。当用户选择再次播放的选项时,是否有办法将数组的值重置为其原始值?

 #include <iostream>
#include <string>
using namespace std;


void contents(); //function to draw the tic tac toe board
char letters[9] = {'a','b','c','d','e','f','g','h','i'}; //array containing
the default characters of the board
string check(); //function to check whether the game is ongoing or has ended
in a win or draw
string result; // string telling us whether the game is ongoing or has ended
in a win or draw





void contents()
{

cout <<endl<<endl<<endl<<" Tic Tac Toe"<<endl<<endl<<endl;


cout << endl;

cout << " | | " << endl;
cout << " " << letters[0] << " | " << letters[1] << " | " << letters[2]
<< endl;

cout << " | | " << endl;
cout << "----- ----- -----" << endl;
cout << " | | " << endl;

cout << " " << letters[3] << " | " << letters[4] << " | " << letters[5]
<< endl;

cout << " | | " << endl;
cout << "----- ----- -----" << endl;
cout << " | | " << endl;

cout << " " << letters[6] << " | " << letters[7] << " | " << letters[8]
<< endl;

cout << " | | " << endl << endl;
}




int main()
{
char selection1; //selection of the mark chosen by player 1 (X or O)
int player = 1;
char mark;//X or O
char selection2; //selection of the character to replace in the array chosen
by the players
char invalid; //character to enter if the user selects a character that
isn't present in the array
string choice;



while(selection1!='X' || selection1!='Y')
{
cout<<"Make your selection player 1 (X or O): ";
cin>>selection1;
if (selection1=='X' || selection1=='O')
break;
else
cout<<"Make a valid selection, press enter to select again"<<endl;
cin.ignore();
cin.get();
}

do
{
player=(player%2)?1:2;

if(selection1 =='X')
mark = (player == 1) ? 'X' : 'O';

else if (selection1=='O')
mark = (player == 1) ? 'O' : 'X';


contents();


cout << "Player " << player << ", enter a letter: ";
cin >> selection2;


if (selection2 == 'a' && letters[0] == 'a')

letters[0] = mark;
else if (selection2 == 'b' && letters[1] == 'b')

letters[1] = mark;
else if (selection2 == 'c' && letters[2] == 'c')

letters[2] = mark;
else if (selection2 == 'd' && letters[3] == 'd')

letters[3] = mark;
else if (selection2 == 'e' && letters[4] == 'e')

letters[4] = mark;
else if (selection2 == 'f' && letters[5] == 'f')

letters[5] = mark;
else if (selection2 == 'g' && letters[6] == 'g')

letters[6] = mark;
else if (selection2 == 'h' && letters[7] == 'h')

letters[7] = mark;
else if (selection2 == 'i' && letters[8] == 'i')

letters[8] = mark;
else
{
cout<<"Invalid move, press enter to continue: ";
cin.ignore();
cin.get();
player--;


}
result=check();

player++;
}while(result=="Ongoing");
contents();
if(result=="Over")
{

cout<<"Player "<<--player<<" wins ";
cin.ignore();
cin.get();
cout<<"Do you want to play again?";
cin>>choice;
if (choice =="Yes")
main();

}
else
{

cout<<"Draw";
cin.ignore();
cin.get();
cout<<"Do you want to play again?";
cin>>choice;
if (choice =="Yes")
main();

}


return 0;
}


string check()
{
if (letters[0] == letters[1] && letters[1] == letters[2])

return "Over";


else if (letters[3] == letters[4] && letters[4] == letters[5])

return "Over";


else if (letters[6] == letters[7] && letters[7] == letters[8])

return "Over";


else if (letters[0] == letters[3] && letters[3] == letters[6])

return "Over";


else if (letters[1] == letters[4] && letters[4] == letters[7])

return "Over";


else if (letters[2] == letters[5] && letters[5] == letters[8])

return "Over";


else if (letters[0] == letters[4] && letters[4] == letters[8])

return "Over";


else if (letters[2] == letters[4] && letters[4] == letters[6])

return "Over";



else if (letters[0] != 'a' && letters[1] != 'b' && letters[2] != 'c'
&& letters[3] != 'd' && letters[4] != 'e' && letters[5] != 'f'
&& letters[6] != 'g' && letters[7] != 'h' && letters[8] != 'i')

return "Draw";


else

return "Ongoing";
}

最佳答案

显而易见的方法是编写一个新函数来完全满足您的需求,即将 letters[0] 设置为 'a' 等。

您稍后可能会了解类。一个类包含多个函数,还有多个数据成员。这种类的每个实例都称为一个对象,您可以将游戏的每一轮实现为一个新对象。

关于c++ - 在 C++ 中重新启动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47533575/

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