我目前正在做一个tic tac toe游戏。我需要制作一个Tic Tac Toe板,每当玩家或计算机移动时更新(显示" X"以及" O"。)。到目前为止,我已经成功登上了董事会,但我不知道如何有效地将用户的输入转换为" X" s。我做的第一件事是像这样的垃圾邮件:
if(playerChoice == 1)
block[0][0] = "X";
if(playerChoice == 2)
block[0][1] = "X";
if(playerChoice == 3)
block[0][2] = "X";
if(playerChoice == 4)
block[1][0] = "X";
if(playerChoice == 5)
block[1][1] = "X";
if(playerChoice == 6)
block[1][2] = "X";
if(playerChoice == 7)
block[2][0] = "X";
if(playerChoice == 8)
block[2][1] = "X";
if(playerChoice == 9)
block[2][2] = "X";
虽然它有效,但它可能是最糟糕的格式。 (现在它有效地将玩家的选择放入正确的方格中。)
这里是完整的代码(它没有完成,甚至可能无效。):
#include <iostream>
#include <ctime>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
//random starting turn chooser
int turnFirst; //variable to decide whoever goes first
int computerRandomPick; //variable to decide which grid the computer will place "O"
srand(time(0));
turnFirst = rand()% (2 - 1 + 1)+1;//generates starting person.
computerRandomPick = rand()% (9 - 1 + 1)+1;//computer first random pick
//board arrays
string block[3][3];
block [0][0] = {" "};
block [0][1] = {" "};
block [0][2] = {" "};
block [1][0] = {" "};
block [1][1] = {" "};
block [1][2] = {" "};
block [2][0] = {" "};
block [2][1] = {" "};
block [2][2] = {" "};
//player interaction
int playerChoice;
//BEGIN OF PROGRAM
cout << "Welcome to Tic Tac Toe!" <<endl<< endl;
if(turnFirst == 1)//player first
{
cout << "Please choose a grid to place (X): "<<endl<<endl;
cout << " 1 | 2 | 3"<< endl;
cout << " -----+-----+-----" << endl;
cout << " 4 | 5 | 6" << endl;
cout << " -----+-----+-----" << endl;
cout << " 7 | 8 | 9" << endl;
cout << " -----+-----+-----" << endl<<endl;
cin >> playerChoice;
system("CLS");
}
if(playerChoice == 1)
block[0][0] = "X";
if(playerChoice == 2)
block[0][1] = "X";
if(playerChoice == 3)
block[0][2] = "X";
if(playerChoice == 4)
block[1][0] = "X";
if(playerChoice == 5)
block[1][1] = "X";
if(playerChoice == 6)
block[1][2] = "X";
if(playerChoice == 7)
block[2][0] = "X";
if(playerChoice == 8)
block[2][1] = "X";
if(playerChoice == 9)
block[2][2] = "X";
if(turnFirst == 2)//computer first
{
system("CLS");
cout << "The computer picked: " <<endl<<endl;
}
if(computerRandomPick == 1)
block[0][0] = "O";
if(computerRandomPick == 2)
block[0][1] = "O";
if(computerRandomPick == 3)
block[0][2] = "O";
if(computerRandomPick == 4)
block[1][0] = "O";
if(computerRandomPick == 5)
block[1][1] = "O";
if(computerRandomPick == 6)
block[1][2] = "O";
if(computerRandomPick == 7)
block[2][0] = "O";
if(computerRandomPick == 8)
block[2][1] = "O";
if(computerRandomPick == 9)
block[2][2] = "O";
//Game Board.
cout << " " << block[0][0] << " | " << block [0][1] << " | " << block [0][2] << endl;
cout << " -----+-----+-----" << endl;
cout << " " << block [1][0] << " | " << block [1][1] << " | " << block [1][2] << endl;
cout << " -----+-----+-----" << endl;
cout << " " << block [2][0] << " | " << block [2][1] << " | " << block [2][2] << endl;
cout << " -----+-----+-----" << endl;
return 0;
}
答案 0 :(得分:4)
假设块[3] [3]声明为:
char block[3][3];
playerChoice可能是1-9。 因此,您可以优化为:
playerChoice --; // to make it 0-8
block[playerChoice / 3][playerChoice %3] = 'X'; // instead of "X"
答案 1 :(得分:0)
这是一个想法:您可以使用网格的常规结构来简化问题。
如果playerChoice
在1到3之间,则第一个索引为0.如果它在4到6之间,则索引为1,如果它在7到9之间,则索引为1是2.这将输入细分为3组,每组3个。事实上,索引是(playerChoice - 1)/3
,具有整数除法。
第二个索引每3个选项重复一次。这意味着使用模数可以帮助到这里。实际上,索引是(playerChoice - 1) % 3
。
您可以将整个代码缩减为
block[(playerChoice-1)/3][(playerChoice-1)%3] = "X";
或某些变化。