Tic-Tac-Toe programming

本文关键字:programming Tic-Tac-Toe | 更新日期: 2023-09-27 18:27:28

我正试图为家庭作业创建一个程序,该程序显示一个Tic-Tac-Toe板,当用户单击按钮时,它会在所有框中显示一个随机数。数字1="0";X〃;并且0="0";O〃;。我创建了9个标签;标签1、标签2…等等";。一旦标签满了,我需要显示谁赢了,字母X或O。我正在使用数组,但在这一点上有点迷失了方向。我需要做什么才能将随机数显示到标签中。以下是迄今为止我为点击事件处理程序编写的代码。

Random rand = new Random(2);
int click;
click = rand.Next(2);
const int ROWS = 3;
const int COLS = 3;
int[,] letters = new int[ROWS,COLS];
int ROW = ROWS;
int COL = COLS;
for (int row = 0; row < ROWS; ROW ++) {
  for (int col = 0; col < COLS; COL ++) {
    letters[row, col] = rand.Next(2);
    int X = 1;//???
    int O = 0;//???
    label1.Text = [ROW,COL].ToString();//???
  }
}

Tic-Tac-Toe programming

这里有一个解释的尝试:

首先,你有数据来表示你的问题:

const int ROWCOUNT = 3;
const int COLCOUNT = 3;
private int[,] letters = new int[ROWCOUNT,COLCOUNT];
Random rand = new Random(DateTime.Now.Ticks);

然后你想随机填充数据:

private void randomize()
{
    for( int row = 0; row < ROWCOUNT; row++ ){ //start with row=0, do row=row+1 until row no longer < ROWCOUNT
      for( int col = 0; col < COLCOUNT; col++ ){
         letters[row,col] = rand.nextInt(2);
      }
    }
}

最后,您想在某个地方显示数组(在您的案例中是标签):

//These need to be added to a GridLayoutManager
private JLabel[,] labels = new JLabel[ROWCOUNT,COLCOUNT];
private void updateView(){
    for( int row = 0; row < ROWCOUNT; row++ ){ //start with row=0, do row=row+1 until row no longer < ROWCOUNT
      for( int col = 0; col < COLCOUNT; col++ ){
         var current = letters[row,col];
         var labelText = "O";
         if( current > 0 ) 
            labelText = "X";
         labels[row,col].Text = labelText;
      }
    }
}

因此,当用户点击按钮时,您调用:

randomize();
updateView();

希望它能帮助

从你的评论来看,设置标签文本似乎需要更多的解释:

     var labelText = "O";
     if( current > 0 ) 
        labelText = "X";
     labels[row,col].Text = labelText;

也许,我应该写得更像这样:

     String textForLabel = "O"; //0 represents O, 1 represents X
     //test to see, if it really is a 0, not a 1
     if( current != 0 ){
        //oh, it is not a zero, therefore, set 
        textForLabel = "X";
     }
     JLabel labelAtRowAndCol = labels[row,col];
     labelAtRowAndCol.Text = textForLabel;

我拒绝给你提供确切的答案,因为你学习如何点他的是这项卓越的全部意义。

在我开始游戏之前,我会随机选择第一步:X或O.

然后我会做以下操作:

1) 我会把所有的标签放在一个集合里。2) 我会随机选择集合中的一个标签,然后更改Text属性。3) 然后我会从集合中删除相同的标签4) 冲洗并重复。

你不需要一个二维数组。

为了找出获胜者。。。我会记录每个球员的动作。在这个游戏中只有一个静态的获胜动作数。这将是一个简单的任务,以确定是否有三个X在顶行。

#include<iostream>
#include<iomanip>
#include<set>
using namespace std;
    char s[3][3] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
    void show(char os[3][3]);
    int def[9];
    void changeo(int n);
    void changex(int n);
    int stop();
    set<int> cset;
    int search (int n){
    }
int main(){
    int n;  show(s);
    int ss = 2;
    cout<<endl;
    while (stop()){
        if (ss%2==0){
            cout<<"player One(O) : enter n "; cin>>n;
            if (!cset.count(n) && n<10){
            cset.insert(n);
            changeo(n);
            show(s);
            ss++;
            }
            else{
                cout<<"invalid move"<<endl;
            }
        }
        else{
            cout<<"player Two(X) : enter n "; cin>>n;
            if (!cset.count(n)&& n<10){
            cset.insert(n);
            changex(n);
            show(s);
            ss++;   
            }
        }

    }
    cout<<"'nyou can see the winner"<<endl;
    cout<<"your moves are "<<ss;
    return 0;
}
void show(char s[3][3]){
    cout<< setw(7)<< "1: " <<s[0][0]<<setw(5)<<"2: " <<s[0][1]<<setw(5)<<"3: " <<s[0][2]<<endl;
    cout<< setw(7)<< "4: " <<s[1][0]<<setw(5)<<"5: " <<s[1][1]<<setw(5)<<"6: " <<s[1][2]<<endl;
    cout<< setw(7)<< "7: " <<s[2][0]<<setw(5)<<"8: " <<s[2][1]<<setw(5)<<"9: " <<s[2][2]<<endl;
    cout<<endl;
}
void changeo(int n){
    switch(n){
        case 1:
            s[0][0] = 'O';
            break;
        case 2:
            s[0][1] = 'O';
            break;
        case 3:
            s[0][2] = 'O';
            break;
        case 4:
            s[1][0] = 'O';
            break;
        case 5:
            s[1][1] = 'O';
            break;
        case 6:
            s[1][2] = 'O';
            break;
        case 7:
            s[2][0] = 'O';
            break;
        case 8:
            s[2][1] = 'O';
            break;
        case 9:
            s[2][2] = 'O';
            break;
    }
}
void changex(int n){
    switch(n){
        case 1:
            s[0][0] = 'X';
            break;
        case 2:
            s[0][1] = 'X';
            break;
        case 3:
            s[0][2] = 'X';
            break;
        case 4:
            s[1][0] = 'X';
            break;
        case 5:
            s[1][1] = 'X';
            break;
        case 6:
            s[1][2] = 'X';
            break;
        case 7:
            s[2][0] = 'X';
            break;
        case 8:
            s[2][1] = 'X';
            break;
        case 9:
            s[2][2] = 'X';
            break;
    }
}
int stop(){
    int m=0;
        for (int i=0; i<3; i++){
            for (int j=0; j<3; j++){
                if(s[i][j]=='*'){
                    m=1;
                    break;
                }
            }
        }
        return m;
    }