简体   繁体   English

我的井字游戏中的 Function 中的数组未更新

[英]Array not Updating in a Function in my tic-tac-toe game

So I am making a tic-tac-toe game in c++.所以我在 c++ 中制作井字游戏。 Here is my code so far:到目前为止,这是我的代码:


#include <iostream>
#include <cmath>

using namespace std;

char gameboard[3][3]{{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; //Declares the Global Array for the Board

char turn = 'X';
int column = 0;
int row = 0;


void PrintBoard() { //Displays the board
    
    cout << "    |    |  \n";
    cout << " "<<gameboard[0][0]<<"  | " <<gameboard[0][1]<<"  | " <<gameboard[0][2]<< " \n";
    cout << "____|____|____\n";
    cout << "    |    |    \n";
    cout << " "<<gameboard[1][0]<<"  | " <<gameboard[1][1]<<"  | " <<gameboard[1][2]<< " \n";
    cout << "____|____|____\n";
    cout << "    |    |    \n";
    cout << " "<<gameboard[2][0]<<"  | " <<gameboard[2][1]<<"  | " <<gameboard[2][2]<< " \n";
    cout << "    |    |    \n";
}

void PlaceCounter(int TileNum, char Tile) {
    int LocalRow = 0;
    int LocalColumn = 0;
    LocalRow = (floor(Tile/3))-1;
    LocalColumn = (TileNum - LocalRow) - 1;
    gameboard[LocalRow][LocalColumn] = Tile;
    PrintBoard();
}

void RunTurn(char playerturn) {
    char LocalTurnTile = ' ';
    switch(turn) {
        case 'X':
            cout << "It is X's turn. Enter the number corresponding to the tile you want to select: ";
            cin >> LocalTurnTile;
            PlaceCounter(LocalTurnTile, playerturn);
            break;
        case 'O':
            cout << "It is O's turn. Enter the number corresponding to the tile you want to select: ";
            cin >> LocalTurnTile;
            PlaceCounter(LocalTurnTile, playerturn);
            break;
    }
    
    
}

void GameLoop() {
    RunTurn(turn);
    
}


int main() //Main Function
{
    PrintBoard();
    GameLoop();
}

The issue is, whenever the function Print Board is called upon for the second time, in the function PlaceCounter, in order to update the screen, it doesn't change, and displays the same thing.问题是,每当第二次调用 function 打印板时,在 function PlaceCounter 中,为了更新屏幕,它不会改变,而是显示相同的东西。 Here is the output这是 output

    |    |  
 1  | 2  | 3 
____|____|____
    |    |    
 4  | 5  | 6 
____|____|____
    |    |    
 7  | 8  | 9 
    |    |    
It is X's turn. Enter the number corresponding to the tile you want to select: 5
    |    |  
 1  | 2  | 3 
____|____|____
    |    |    
 4  | 5  | 6 
____|____|____
    |    |    
 7  | 8  | 9 
    |    |    

I don't know what shouldn't be working.我不知道什么不应该工作。 The 5 square (center) should update to an X, but it isn't. 5 方格(中心)应该更新为 X,但事实并非如此。 I am quite new to c++ and need some help.我对 c++ 很陌生,需要一些帮助。 If anyone knows what's going on, I would love your help.如果有人知道发生了什么,我会很高兴你的帮助。 I've looked on the internet, and no one has had the same issue as me before.我上网查了一下,没有人遇到过和我一样的问题。 I looked all through the code, and can't seem to put my finger on what isn't working.我查看了所有代码,似乎无法确定什么不起作用。

Your math is out and you have a typo (wrong variable)你的数学已经出来了,你有一个错字(错误的变量)

This is the correct code (I'm assuming TileNum has a values from 1 to 9)这是正确的代码(我假设TileNum的值从 1 到 9)

LocalRow = (TileNum - 1)/3;
LocalColumn = (TileNum - 1)%3;

Note that when you divide one integer by another you always get another integer, so floor is unnecessary.请注意,当您将一个 integer 除以另一个时,您总是会得到另一个 integer,因此不需要floor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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