简体   繁体   中英

C++: How to take user input and put it into Tic Tac Toe board

I'm currently working on a tic tac toe game. I need to make a Tic Tac Toe board which updates(shows the "X" and the "O".) every time the player or the computer makes a move. So far, I've managed to make the board but I don't know how to efficiently turn the user's input into "X"s. The first thing I did was spam something like this:

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";

Although It works, it is probably the worst format possible. (Right now it effectively puts the player's choice into the correct squares.)

Here's the full code(It's not completed and might not even work.):

#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;
}

Presuming block[3][3] declared as:

char block[3][3];

playerChoice could be 1-9. So, you may optimize as:

playerChoice --; // to make it 0-8
block[playerChoice / 3][playerChoice %3] = 'X'; // instead of "X"

Here's an idea: You can use the regular structure of a grid to simplify the problem.

If playerChoice is between 1 and 3, the first index is 0. If it's between 4 and 6, the index is 1, and if it's between 7 and 9 the index is 2. This subdivides the inputs into 3 groups of 3. In fact, the index is (playerChoice - 1)/3 with integer division.

The second index repeats every 3 choices. That means using modulus could help here. In fact, the index is (playerChoice - 1) % 3 .

You can reduce the entire code to

block[(playerChoice-1)/3][(playerChoice-1)%3] = "X";

or some variation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM