# Problems determining Winner: TicTacToe.

Page 1 of 1

## 5 Replies - 1090 Views - Last Post: 29 April 2012 - 01:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=277341&amp;s=1879ed08e5a17ff887c9cc5a25bcd091&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 imckinn40

Reputation: 0
• Posts: 41
• Joined: 19-March 12

# Problems determining Winner: TicTacToe.

Posted 29 April 2012 - 09:39 AM

I'm doing a Tic Tac Toe code that call the winner or a draw. I have everything but I can't get the winner or draw done. I'm not sure where to put it or what I'm missing. I tried some things but keep getting errors. This is what I have so far, I removed, what I thought was the code for winner or draw.

```import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class TicTacToe extends JPanel implements ActionListener {

JButton btnButton1, btnButton2, btnButton3, btnButton4, btnButton5, btnButton6, btnButton7,btnButton8, btnButton9;
JPanel panel1, panel2, panel3;
JTextField txtMove=new JTextField(10);
int turn =1; // 1 is O 2 X
int cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9;

public TicTacToe() {

setLayout(new GridLayout(3,3));
panel1= new JPanel();
panel2= new JPanel();
panel3= new JPanel();

panel1.setLayout(new GridLayout(3,3));

cell1=cell2=cell3=cell4=cell5=cell6=cell7=cell8=cell9=0;

btnButton1=new JButton(" ");
btnButton2=new JButton(" ");
btnButton3=new JButton(" ");
btnButton4=new JButton(" ");
btnButton5=new JButton(" ");
btnButton6=new JButton(" ");
btnButton7=new JButton(" ");
btnButton8=new JButton(" ");
btnButton9=new JButton(" ");
btnButton1.setActionCommand("cell1");
btnButton2.setActionCommand("cell2");
btnButton3.setActionCommand("cell3");
btnButton4.setActionCommand("cell4");
btnButton5.setActionCommand("cell5");
btnButton6.setActionCommand("cell6");
btnButton7.setActionCommand("cell7");
btnButton8.setActionCommand("cell8");
btnButton9.setActionCommand("cell9");

setPreferredSize(new Dimension(300,300));
}

public void actionPerformed(ActionEvent event) {
System.out.println(event.getActionCommand());
String s=event.getActionCommand();
String txtCaption=" ";
if(turn==1) txtCaption="O";
else
if(turn==2) txtCaption="X";

if(s=="cell1" && cell1==0) {
btnButton1.setText(txtCaption);
cell1=turn;
}
if(s=="cell2" && cell2==0) {
btnButton2.setText(txtCaption);
cell2=turn;
}
if(s=="cell3" && cell3==0) {
btnButton3.setText(txtCaption);
cell3=turn;
}
if(s=="cell4" && cell4==0) {
btnButton4.setText(txtCaption);
cell4=turn;
}
if(s=="cell5" && cell5==0) {
btnButton5.setText(txtCaption);
cell5=turn;
}
if(s=="cell6" && cell6==0) {
btnButton6.setText(txtCaption);
cell6=turn;
}
if(s=="cell7" && cell7==0) {
btnButton7.setText(txtCaption);
cell7=turn;
}
if(s=="cell8" && cell8==0) {
btnButton8.setText(txtCaption);
cell8=turn;
}
if(s=="cell9" && cell9==0) {
btnButton9.setText(txtCaption);
cell9=turn;
}

if(turn==1) turn=2;
else
if(turn==2) turn=1;

}
}
```

*Edited: topic title renamed to be more meaninful

This post has been edited by pbl: 29 April 2012 - 09:58 AM

Is This A Good Question/Topic? 0

## Replies To: Problems determining Winner: TicTacToe.

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Problems determining Winner: TicTacToe.

Posted 29 April 2012 - 10:02 AM

You should learn how to use arrays, that will simplify to life a lot.
If you have an array JButton[3][3]:
- first they can be initialized in a loop
- testing in the actionPerformed() is also done with a loop
your number of lines of code will be cut at least by 5

You can also have an array of int[3][3]
put 10 in that array when there is a X
put 100 in that array when there is a O

When the total of a row, or column or diagnonal is 300 the O won
When the total of a row, or column or diagnonal is 30 the x won

### #3 imckinn40

Reputation: 0
• Posts: 41
• Joined: 19-March 12

## Re: Problems determining Winner: TicTacToe.

Posted 29 April 2012 - 10:18 AM

So I have to first determine the winner and the it will print out "winner" or "draw". I think I'm trying to print winner when there is a winner or draw if there no winner.

### #4 imckinn40

Reputation: 0
• Posts: 41
• Joined: 19-March 12

## Re: Problems determining Winner: TicTacToe.

Posted 29 April 2012 - 10:42 AM

Thanks for changing my topic. This is my assignment, create a 3x3 grid (I have that). there are two players, O and X. Player O goes first (I have that) then X alternating until either all cells(9) are filled or there are 3X's or O's diagonally, horizontally or vertically. Call the winner or a draw. I have everything but the Call the winner or a draw.

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Problems determining Winner: TicTacToe.

Posted 29 April 2012 - 01:22 PM

Without an array it will be nightmare and you will have code like

```int winningNumber() {
// rows
if(cell1 != 0 && cell1 == cell2 && cell2 == cell3)
return cell1;
if(cell4 != 0 && cell4 == cell5 && cell5 == cell6)
return cell4;
if(cell7 != 0 && cell7 == cell8 && cell8 == cell9)
return cell7;
// column
if(cell1 != 0 && cell1 == cell4 && cell4 == cell7)
return cell1;
... second column
.... thirs column

// diagonals
if(cell1 != 0 & cell1 == cell5 && cell5 == cell9)
return cell1;
.. second diagonal

return 0;    // no winner yet
}

```

### #6 imckinn40

Reputation: 0
• Posts: 41
• Joined: 19-March 12

## Re: Problems determining Winner: TicTacToe.

Posted 29 April 2012 - 01:42 PM

Again thank you for all your help. From what you see can you tell me where to start, I'm very new to this, I'm lost on where to start and what I'm missing. Thanking you in advance.