我对C++来说很新颖。C++井字游戏程序优化
我刚完成我的第一个“真实”程序。
这是一个井字游戏。
是否有人愿意阅读槽我的代码告诉我在哪里我可以优化一些东西?
#include <iostream>
using namespace std;
/**< Board variables */
string a1 = "_"; string a2 = "_"; string a3 = "_";
string b1 = "_"; string b2 = "_"; string b3 = "_";
string c1 = "_"; string c2 = "_"; string c3 = "_";
string showboard(){
cout << '\t' << ""<< '\t' << "1" << '\t' << "2" << '\t' << "3" << "\n";
cout << '\t' << "a"<< '\t' << a1 << '\t' << a2 << '\t' << a3 << "\n";
cout << '\t' << "b"<< '\t' << b1 << '\t' << b2 << '\t' << b3 << "\n";
cout << '\t' << "c"<< '\t' << c1 << '\t' << c2 << '\t' << c3 << "\n\n";
return "";
};
/**< Turn */
int turn;
/**< useless variable */
int x =1;
/**< "tiles" for example "X" or "O" */
string boardvalue;
/**< Checks if Turn is valid/legal */
string turnvalidation (string boardvalue){
if(turn==1 && a1!="_"){
cout << "Invalid option. Choose again! \n\n";
x=1;
} else if(turn==2 && a2!="_"){
cout << "Invalid option. Choose again! \n\n";
x=1;
} else if(turn==3 && a3!="_"){
cout << "Invalid option. Choose again! \n\n";
x=1;
} else if(turn==4 && b1!="_"){
cout << "Invalid option. Choose again! \n\n";
x=1;
} else if(turn==5 && b2!="_"){
cout << "Invalid option. Choose again! \n\n";
x=1;
} else if(turn==6 && b3!="_"){
cout << "Invalid option. Choose again! \n\n";
x=1;
} else if(turn==7 && c1!="_"){
cout << "Invalid option. Choose again! \n\n";
x=1;
} else if(turn==8 && c2!="_"){
cout << "Invalid option. Choose again! \n\n";
x=1;
} else if(turn==9 && c3!="_"){
cout << "Invalid option. Choose again! \n\n";
x=1;
} else if(turn==10){
cout << "Invalid option. Choose again! \n\n";
x=1;
} else {
x=0;
switch (turn){
case 1: a1=boardvalue; break;
case 2: a2=boardvalue; break;
case 3: a3=boardvalue; break;
case 4: b1=boardvalue; break;
case 5: b2=boardvalue; break;
case 6: b3=boardvalue; break;
case 7: c1=boardvalue; break;
case 8: c2=boardvalue; break;
case 9: c3=boardvalue; break;
}
}
return"";
};
/**< Checks if the game is won */
int wincheck(string boardvalue){
// Rows
if(a1==boardvalue && a2==boardvalue && a3==boardvalue){return 0;}else
if(b1==boardvalue && b2==boardvalue && b3==boardvalue){return 0;}else
if(c1==boardvalue && c2==boardvalue && c3==boardvalue){return 0;}else
// Columns
if(a1==boardvalue && b1==boardvalue && c1==boardvalue){return 0;}else
if(a2==boardvalue && b2==boardvalue && c2==boardvalue){return 0;}else
if(a3==boardvalue && b3==boardvalue && c3==boardvalue){return 0;}else
// Diagonals
if(c1==boardvalue && b2==boardvalue && a3==boardvalue){return 0;}else
if(a1==boardvalue && b2==boardvalue && c3==boardvalue){return 0;}else
{return 1;};
}
int main()
{
string player1;
string player2;
cout << "Player 1 enter your name \n";
cin >> player1;
cout << "\nPlayer 2 enter your name \n";
cin >> player2;
cout << endl;
cout << "Input example: a1 [enter]\n\n";
string turni; /**< Converts Input in switch value! */
while(1){ /**< Play again */
string a1 = "_"; string a2 = "_"; string a3 = "_";
string b1 = "_"; string b2 = "_"; string b3 = "_";
string c1 = "_"; string c2 = "_"; string c3 = "_";
while(1){
/**< Player 1s turn */
x=1;
while(1){
cout << player1 << " place your X \n\n";
showboard();
cin >> turni;
if(turni=="a1"){turn=1;} else if(turni=="a2"){turn=2;} else if(turni=="a3"){turn=3;} else if(turni=="b1"){turn=4;} else if(turni=="b2"){turn=5;} else if(turni=="b3"){turn=6;} else if(turni=="c1"){turn=7;} else if(turni=="c2"){turn=8;} else if(turni=="c3"){turn=9;} else{turn=10;};
/**< Checks if Turn is valid/legal */
turnvalidation("X");
break;
}
/**< Win Check */
if(wincheck("X") == 0){
showboard();
cout << "Congratulations " << player1 << " you won!";
break;
};
/**< Player 1s turn is over */
/**< Player 2s turn */
x=1;
while(1){
cout << player2 << " place your 0 \n\n";
showboard();
cin >> turni;
if(turni=="a1"){turn=1;} else if(turni=="a2"){turn=2;} else if(turni=="a3"){turn=3;} else if(turni=="b1"){turn=4;} else if(turni=="b2"){turn=5;} else if(turni=="b3"){turn=6;} else if(turni=="c1"){turn=7;} else if(turni=="c2"){turn=8;} else if(turni=="c3"){turn=9;} else{turn=10;};
/**< Checks if Turn is valid/legal */
turnvalidation("O");
break;
}
/**< Win Check */
if(wincheck("O")==0){
showboard();
cout << "Congratulations " << player2 << " you won!";
break;
};
/**< Player 1s turn is over */
}
/**< Play again and clear board */
cout << "\n\nIf you want to play again type ""1""! \n";
cin >> x;
if(x==1){
a1 = "_"; a2 = "_"; a3 = "_";
b1 = "_"; b2 = "_"; b3 = "_";
c1 = "_"; c2 = "_"; c3 = "_";
} else {break;}
}
}
游戏本身的作品。
但
在本节是一个错误:
cout << "\n\nIf you want to play again type ""1""! \n";
cin >> x;
if(x==1){
a1 = "_"; a2 = "_"; a3 = "_";
b1 = "_"; b2 = "_"; b3 = "_";
c1 = "_"; c2 = "_"; c3 = "_";
} else {break;}
你赢得进入此节。
如果您输入“1”,游戏将重新启动并应清除棋盘。
每隔一个输入将关闭该程序。
游戏将重新启动,但棋盘不会清除。
如果您的代码正常工作,而您只是在寻找同行评审,则应该在[codereview.se]上发帖。这个网站是关于您遇到的问题的问题,而不是*您能否为我看看这个问题?*问题。 –
您声明了9个变量而不是某种数组或结构,并且强制您的代码通过它进行大量重复。当你的代码开始看起来像是所有的东西都被复制/粘贴时,这表明你做错了什么。 –
你很幸运井字棋只有9个位置。学习使用数组。 – PaulMcKenzie