Programación Tic-Tac-Toe

I'm trying to create a program for homework that displays a Tic-Tac-Toe board, and when the user clicks the button it displays a random number in all of the boxes. The number 1 = "X" and 0 = "O". I created 9 labels labeled "label1, label2...etc". Once the labels are full, I need to display who won, the letter X or O. I'm using arrays for this but am kinda of lost at this point. what do I need to do to display the random numbers into the labels. Here is the code I've written for the click event handler so far.

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();//???
}
}

preguntado el 09 de marzo de 12 a las 16:03

I'm not sure I understand the requirements. There is nothing about the game of Tic-Tac-Toe that involves random numbers. -

You need to have a flag set for which players turn it is, then you can set the value of the button to X or O depending on whose turn it currently is. You should then check if any player has 3 in a row and wins after that move is made. -

@Jonathan Wood: It's tic-tac-toe but with 1 for X and 0 for O. -

@ Jonathan Wood, @jzworkman No one is actually playing, When the "Start Game" button is clicked, it automatically plays a game, and displays who won. -

You need to post more of your code. You need to also think about trying to clarify your question. -

3 Respuestas

Here an attempt at an explanation:

first, you have the data to represent your problem:

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

then you want to randomly fill that data:

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

finally, you want to display the array somewhere (in your case labels):

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

so, when the user clicks the button, you call:

randomize();
updateView();

Espero eso ayude

from your comments, it seems setting the Label Text needs more explanation:

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

maybe, i should have written it more like this:

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;

Respondido el 22 de Septiembre de 12 a las 23:09

to figure out who won, create another method and use the same loop, counting the 0 and 1 by row or column - mente y medios

Thank you for your help, but to be honest I'm new at this, what would I just need to do from using my code to display the different x or o's in the labels I created. That part has me stuck. Also what can I do since I can't explicitly set "string X = 1"??? I've never heard or used a Jlabel before. Ill be able to figure out to display who won after I can figure this part out. Thank you for your help - EASTON

I made a few adjustments. I'm thinking about using a cast for the letter x and o. As for, how to display the different letters in the label, I'm still lost. - EASTON

for(int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { letters[ROWS, COLS] = rand.Next(2); int anX = 1 int anO = 0 float O = an0 float X = anX - EASTON

I refuse to provide you the exact answer since you learning how to dot his is the entire point of this excerise.

Before I started the game I would randomly choose the first move: X or O.

I would then do the following:

1) I would place all the Labels into a collection. 2) I would randomly choose one of the Labels within the collection and change the Text property. 3) I would then remove the same Label from the collection 4) Rinse and Repeat.

You DO NOT need a two diminsional array for this.

In order to figure out the winner...I would keep track of the moves of each player. There is only a static number of winning moves in this game. Would be a simple task to determine if there were three X's in the top row or not.

respondido 09 mar '12, 17:03

It states in the instructions to use a 2d array. Also no one is playing the game its a simulator. - EASTON

#include<iostream>
#include<iomanip>
#include<set>
using namespace std;

char s = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
void show(char os);
int def;
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;
return 0;
}

void show(char s){
cout<< setw(7)<< "1: " <<s<<setw(5)<<"2: " <<s<<setw(5)<<"3: " <<s<<endl;
cout<< setw(7)<< "4: " <<s<<setw(5)<<"5: " <<s<<setw(5)<<"6: " <<s<<endl;
cout<< setw(7)<< "7: " <<s<<setw(5)<<"8: " <<s<<setw(5)<<"9: " <<s<<endl;
cout<<endl;
}
void changeo(int n){
switch(n){
case 1:
s = 'O';
break;
case 2:
s = 'O';
break;
case 3:
s = 'O';
break;
case 4:
s = 'O';
break;
case 5:
s = 'O';
break;
case 6:
s = 'O';
break;
case 7:
s = 'O';
break;
case 8:
s = 'O';
break;
case 9:
s = 'O';
break;
}
}

void changex(int n){
switch(n){
case 1:
s = 'X';
break;
case 2:
s = 'X';
break;
case 3:
s = 'X';
break;
case 4:
s = 'X';
break;
case 5:
s = 'X';
break;
case 6:
s = 'X';
break;
case 7:
s = 'X';
break;
case 8:
s = 'X';
break;
case 9:
s = '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;
}

Respondido el 22 de Septiembre de 13 a las 10:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.