简体   繁体   English

C ++井字游戏用户输入分配

[英]C++ Tic-Tac-Toe User Input Assignment

For some practice I decided to make a console tic-tac-toe game. 为了进行一些练习,我决定制作一个台式井字游戏。 Here is a snippit from int main 这是来自int main的摘录

int main()
{
    //Small 2x2 board (for now), hense the 4 options below
    int op1, op2, op3, op4;

    //Draws the board on the screen, separate function I've made
    board(op1, op2, op3, op4); 

    //asking the user the first option, which is for X not O
    //User will enter 1, 2, 3, or 4 and an X should appear in corresponding quadrant.
    cout << "x? ";
    cin >> op1;
}

So what I want to happen is for the board to appear first, and then afterwards have the console ask the user to enter in where to put the X's and the O's. 因此,我想让板首先出现,然后让控制台要求用户输入将X和O放在哪里。

But what ends up happening here is that the user's option doesn't show up on the board, because the user enters in the option after the board has been loaded rather than before. 但是最终发生的事情是用户的选项未显示在板上,因为用户在加载板之后而不是之前输入了该选项。

How can I overwrite the original assignment of int op1 to the user's input so that it automatically places it on the board? 如何覆盖int op1对用户输入的原始分配,以便将其自动放置在板上? Thanks! 谢谢!

PS: I didn't want to cheat an look up source code of a tic-tac-toe game :P PS:我不想欺骗井字游戏的源代码:P

You can loop over the user input and update the board state with the latest input by calling your function board again. 您可以遍历用户输入,并通过再次调用功能板来用最新输入更新板状态。 Also, I think creating a 2x2 tic-tac-toe isn't a good idea to learn about games because any two moves will end the game. 另外,我认为创建2x2井字游戏不是学习游戏的好主意,因为任何两个动作都会结束游戏。 Instead try the standard 3x3 tic tac toe. 而是尝试使用标准的3x3井字游戏。 For this you'll have to implement a new function that checks if the game has been won by a player. 为此,您必须实现一个新功能,该功能可以检查玩家是否赢得了比赛。

You can use system("CLS") to clear the screen after the user input, cin. 用户输入cin后,可以使用system(“ CLS”)清除屏幕。 and cout after system("CLS"), to display the new edited board with the user's input. 并在system(“ CLS”)之后点击,以显示带有用户输入的新编辑板。 This just solves the board to be updated with the user's input :) 这只是解决了要用用户输入更新的板:)

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

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