在定义字符数组的 for 循环内应用输入限制(井字游戏)

Posted

技术标签:

【中文标题】在定义字符数组的 for 循环内应用输入限制(井字游戏)【英文标题】:Applying input limits inside a for loop defining a char array (Tic Tac Toe) 【发布时间】:2020-01-26 07:25:41 【问题描述】:

这是我的初始代码 - 用户可以使用它来玩井字游戏,方法是指定他们想要在 char 数组中添加零或交叉的位置 -

#include <stdio.h>
#include <iostream>
using namespace std;

int main()

   char a[3][3] = '1', '2', '3', '4', '5', '6', '7', '8', '9';
   cout << (char)a[0][0] << "    " << (char)a[0][1] << "    " << (char)a[0][2] << endl << endl << endl;
   cout << (char)a[1][0] << "    " << (char)a[1][1] << "    " << (char)a[1][2] << endl << endl << endl;
   cout << (char)a[2][0] << "    " << (char)a[2][1] << "    " << (char)a[2][2] << endl << endl << endl;

   int x, y;

   for (int i = 0; i < 5; i ++)
   
       cout << "Input x: ";
       cin >> x;
       cout << "Input y: ";
       cin >> y;

       system("clear");

       a[x-1][y-1] =  'X';

       cout << (char)a[0][0] << "    " << (char)a[0][1] << "    " << (char)a[0][2] << endl << endl << endl;
       cout << (char)a[1][0] << "    " << (char)a[1][1] << "    " << (char)a[1][2] << endl << endl << endl;
       cout << (char)a[2][0] << "    " << (char)a[2][1] << "    " << (char)a[2][2] << endl << endl << endl;

       cout << "Input x: ";
       cin >> x;
       cout << "Input y: ";
       cin >> y;

       system("clear");

       a[x-1][y-1] =  'O';

       cout << (char)a[0][0] << "    " << (char)a[0][1] << "    " << (char)a[0][2] << endl << endl << endl;
       cout << (char)a[1][0] << "    " << (char)a[1][1] << "    " << (char)a[1][2] << endl << endl << endl;
       cout << (char)a[2][0] << "    " << (char)a[2][1] << "    " << (char)a[2][2] << endl << endl << endl;
   

为了避免用户输入[1,3]之外的数组索引,我尝试实现一个if条件

if(x <= 0 && x > 3)

    cout << "Wrong! Enter again. Input x: ";
    cin >> x;

它不工作。由于 for 循环对 X 和 O 各迭代 5 次,因此输出要么卡在“输入 X”阶段,要么在 5 个“输入 X”后退出。

这是我尝试过的——最终接受了 5 个 X 输入,然后就退出了——

#include <stdio.h>
#include <iostream>
using namespace std;

int main()

   char a[3][3] = '1', '2', '3', '4', '5', '6', '7', '8', '9';
   cout << (char)a[0][0] << "    " << (char)a[0][1] << "    " << (char)a[0][2] << endl << endl << endl;
   cout << (char)a[1][0] << "    " << (char)a[1][1] << "    " << (char)a[1][2] << endl << endl << endl;
   cout << (char)a[2][0] << "    " << (char)a[2][1] << "    " << (char)a[2][2] << endl << endl << endl;

   int x, y;

   for (int i = 0; i < 5; i ++)
   
       cout << "Input x: ";
       cin >> x;
       if(x <= 0 && x > 3)
       
           cout << "Wrong! Enter again. Input x: ";
           cin >> x;
           continue;
       
       else
       
           continue;
       

       cout << "Input y: ";
       cin >> y;
       if(y <= 0 && y > 3)
       
           cout << "Wrong! Enter again. Input y: ";
           cin >> y;
           continue;
       
       else
       
           continue;
       


       /*
       if(x <= 0 && x > 3)
       
           cout << "Wrong! Enter again. Input x: ";
           cin >> x;
       

       if(y <= 0 && y > 3)
       
           cout << "Wrong! Enter again. Input y: ";
           cin >> y;
       
       */

       system("clear");

       a[x-1][y-1] =  'X';

       cout << (char)a[0][0] << "    " << (char)a[0][1] << "    " << (char)a[0][2] << endl << endl << endl;
       cout << (char)a[1][0] << "    " << (char)a[1][1] << "    " << (char)a[1][2] << endl << endl << endl;
       cout << (char)a[2][0] << "    " << (char)a[2][1] << "    " << (char)a[2][2] << endl << endl << endl;

       cout << "Input x: ";
       cin >> x;
       if(x <= 0 && x > 3)
       
           cout << "Wrong! Enter again. Input x: ";
           cin >> x;
           continue;
       
       else
       
           continue;
       


       cout << "Input y: ";
       cin >> y;
       if(y <= 0 && y > 3)
       
           cout << "Wrong! Enter again. Input y: ";
           cin >> y;
           continue;
       
       else
       
           continue;
       


       /*
       if(x <= 0 && x > 3)
       
           cout << "Wrong! Enter again. Input x: ";
           cin >> x;
       

       if(y <= 0 && y > 3)
       
           cout << "Wrong! Enter again. Input y: ";
           cin >> y;
       
       */
       system("clear");

       a[x-1][y-1] =  'O';

       cout << (char)a[0][0] << "    " << (char)a[0][1] << "    " << (char)a[0][2] << endl << endl << endl;
       cout << (char)a[1][0] << "    " << (char)a[1][1] << "    " << (char)a[1][2] << endl << endl << endl;
       cout << (char)a[2][0] << "    " << (char)a[2][1] << "    " << (char)a[2][2] << endl << endl << endl;
   

为什么我不能只在输入后有一个 if 语句,其中它检查它是否超出范围,如果是,它可以再次询问用户,然后继续? - 这是我想要实现的。

【问题讨论】:

提示:for loop 正在迭代 5 次,无论用户输入是否错误。您需要继续游戏,直到一位用户success 或所有瓷砖都被填满。甚至你可以为输入添加一个循环循环,直到它有效为止。 【参考方案1】:

我认为正确的运算符是 ||而不是 &&。

通过使用 &&,您建议必须同时发生 x3 才能采用 IF 语句!

尝试用英语大声朗读你的陈述会有所帮助,有助于你更好地理解它:)

【讨论】:

【参考方案2】:

为什么不能只在输入后有一个if语句,检查是否越界,如果是,可以再次询问用户,然后继续?

if 语句检查用户输入的有效性,但在输入有效之前它不会迭代。您需要有一个loop,它会一直运行到用户输入有效为止。

如下所示,

#include <iostream>

bool isGameCompleat(char a[3][3])

   /*check for all combination for the board for game complete*/
    if ((a[0][0] == a[0][1] && a[0][0] == a[0][2]) ||
        (a[1][0] == a[1][1] && a[1][0] == a[1][2]) ||
        (a[2][0] == a[2][1] && a[2][0] == a[2][2]) ||
        (a[0][0] == a[1][0] && a[0][0] == a[2][0]) ||
        (a[0][1] == a[1][1] && a[0][1] == a[2][1]) ||
        (a[0][3] == a[1][3] && a[0][3] == a[2][3]) ||
        (a[0][0] == a[1][1] && a[0][0] == a[2][2]) ||
        (a[0][3] == a[1][1] && a[0][3] == a[2][0]))
        return true;
    return false;


void printBoard(char a[3][3])

    std::cout << (char)a[0][0] << "    " << (char)a[0][1] << "    " << (char)a[0][2] << "\n\n\n";
    std::cout << (char)a[1][0] << "    " << (char)a[1][1] << "    " << (char)a[1][2] << "\n\n\n";
    std::cout << (char)a[2][0] << "    " << (char)a[2][1] << "    " << (char)a[2][2] << "\n\n\n";


int main()

    char a[3][3] =  '1', '2', '3', '4', '5', '6', '7', '8', '9' ;

    int x, y;

    printBoard(a);

    for (int i = 0; (i < 9); i++)
    
        int p;
        if ((i & 1) == 0) 
            std::cout << "X Turn ";
        
        else 
            std::cout << "O Turn ";
        

        std::cout << "Enter the position ";
        std::cin >> p;  

        /*derived  row and column from 0 to 9 position*/
        x = (p / 3);
        y = (p%3) -1;

        /*Validity check for input if wrong request for new input
         * out of range check and non-repeated tile is selected
         */
        while (p < 0 || p > 9 || (a[x][y] =='X') || (a[x][y]=='O'))
           
            std::cout << "Wrong! Enter again: ";
            std::cin >> p;
            x = (p / 3);
            y = (p%3) -1;
        

        if ((i & 1) == 0) 
            a[x][y] = 'X';
        
        else 
            a[x][y] = 'O';
        

        system("clear");
        printBoard(a);

        if (isGameCompleat(a))
        
            std::cout <<(  (i & 1) ? "O" : "X") << " wins";
            break;
        
        else if (i == 8)
        
            std::cout << "Game is draw";
                   
    

【讨论】:

以上是关于在定义字符数组的 for 循环内应用输入限制(井字游戏)的主要内容,如果未能解决你的问题,请参考以下文章

对井字游戏获胜者的二维数组进行排序

c语言输出字符串数组出现乱码

如何找到for循环中数组的长度

PTA循环

c语言循环赋值

数组相关知识