/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication11;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Jayson Jude
*/
public class JavaApplication11 implements ActionListener {
private JFrame window = new JFrame("Tic-Tac-Toe");
private JPanel inputPanel = new JPanel();
private JPanel boardPanel = new JPanel();
private JButton button3 = new JButton("3 by 3");
private JButton button4 = new JButton("4 by 4");
private JButton button5 = new JButton("5 by 5");
private JLabel label = new JLabel ("Click a button to choose game board size:");
private JButton buttons[][];
private int boardSize=0;
private int count = -1;
private String letter = "";
private boolean win = false;
public JavaApplication11(){
//Create Window
window.setSize(500,500);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add Label and buttons to inputPanel
inputPanel.add(label);
inputPanel.add(button3);
inputPanel.add(button4);
inputPanel.add(button5);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
inputPanel.setSize(500,200);
window.add(inputPanel,"North");
//Make The Window Visible
window.setVisible(true);
}
/**
When an object is clicked, perform an action.
@param a action event object
*/
public void actionPerformed(ActionEvent a) {
if(count==-1){
letter="-";
count=0;
}
else{
count++;
//Calculate whose turn it is
if(count % 2 == 0){
letter = "O";
} else {
letter = "X";
}
}
if (a.getSource() == button3) {
boardSize=3;
}
else if (a.getSource()== button4){
boardSize=4;
}
else if (a.getSource() == button5){
boardSize=5;
}
if ((a.getSource() == button3)||(a.getSource()==button4)||(a.getSource()==button5)){
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
buttons = new JButton[boardSize][boardSize];
for(int i=0; i<boardSize; i++)
for (int j=0; j<boardSize; j++){
buttons[i][j] = new JButton();
boardPanel.add(buttons[i][j]);
buttons[i][j].addActionListener(this);
}
boardPanel.setSize(300,300);
boardPanel.setLayout(new GridLayout(boardSize,boardSize));
window.add(boardPanel,"Center");
}
//Write the letter to the button and deactivate it
JButton pressedButton = (JButton)a.getSource();
pressedButton.setText(letter);
pressedButton.setEnabled(false);
//Determine winner
//Show a dialog when game is over
if(win == true){
JOptionPane.showMessageDialog(null, letter + " wins the game!");
JOptionPane.showInputDialog("Do you want to play again? Y/N");
char choice = 0;
if (choice=='Y'||choice=='y')
{
new JavaApplication11();
}
else if (choice=='N'||choice=='n')
{
JOptionPane.showMessageDialog(null,"GOODBYE!");
System.exit(0);
}
} else if(count == (boardSize*boardSize) && win == false){
JOptionPane.showMessageDialog(null, "The game was tie!");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new JavaApplication11();
}
}
10 Replies - 437 Views - Last Post: 04 October 2011 - 01:50 PM
#1
can you help me determine a winner to my program?
Posted 03 October 2011 - 07:22 PM
here's my program .. a tic tac toe program .. hope you can help me ..
Replies To: can you help me determine a winner to my program?
#2
Re: can you help me determine a winner to my program?
Posted 03 October 2011 - 07:28 PM
please help me .. i tried my best .. i dont know to compare and array to get a winner .. hope there is someone here that can help me ...
#3
Re: can you help me determine a winner to my program?
Posted 03 October 2011 - 07:42 PM
Hint- take a look at the getText() method for the JButton. How do you compare all the elements in a row? How about for the columns? Now for the diagonals, what indices do you need to evaluate? Think about it that way.
#4
Re: can you help me determine a winner to my program?
Posted 03 October 2011 - 07:46 PM
You could use an array of chars. The size of the array should be the same as the number of boxes. Once a player chooses a box, either add 'x' or 'o' depending on which players turn it is. Then you could do ifs like these:
This is just a simple solution. I have a question for the experts, look at my if conditions, how would I do it so that the three boxes are compared for equality? value1==value2==value3?
if(box[0]==box[1]&&box[0]==box[2]){
System.out.println(box[0]+" WINS!");
}
This is just a simple solution. I have a question for the experts, look at my if conditions, how would I do it so that the three boxes are compared for equality? value1==value2==value3?
#5
Re: can you help me determine a winner to my program?
Posted 03 October 2011 - 09:23 PM
The simplest way have an array of int[3][3] the size of the board
When the button[x][y] becomes "X" put 10 in the corresponding int[][]
When the button[x][y] becomes "O" put 100 in the corresponding int[][]
If the sum of the int[][] in a row, a column or a diagonal is 30 the X won
If the sum of the int[][] in a row, a column or a diagonal is 300 the O won
You can actually combine the array of JButton and int in a single class
Now in your pannel
Happy coding
When the button[x][y] becomes "X" put 10 in the corresponding int[][]
When the button[x][y] becomes "O" put 100 in the corresponding int[][]
If the sum of the int[][] in a row, a column or a diagonal is 30 the X won
If the sum of the int[][] in a row, a column or a diagonal is 300 the O won
You can actually combine the array of JButton and int in a single class
class TTTButton extends JButton implements ActionListener {
int value;
// constructor
TTTButton() {
super(" "); // start with blank
addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(X turn) {
setText("X");
value = 10;
}
else {
setText("O");
value = 100;
}
setEnabled(false);
}
// reset method for next game
void reset() {
setText("");
setEnable(true); // make it available again
}
}
Now in your pannel
class BoardPanel extends JPanel implements ActionListener {
TTTButton[][] button;
BoardPanel() {
super(new GridLayout(3,3);
button = new TTTButton[3][3];
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
button[i][j] = new TTTButton();
button[i][j].addActionListener(this);
add(button[i][j]);
}
}
}
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < 3; ++i) {
int total = 0;
for(int j = 0; j < 3; j++)
total += button[i][j].value;
if(total == 30) ... the X won
if(total == 300) ... the O won
}
for(int j = 0; j < 3; ++j) {
int total = 0;
for(int i = 0; i < 3; i++)
total += button[i][j].value;
if(total == 30) ... the X won
if(total == 300) ... the O won
}
// I'll let you do the diagonals
Happy coding
#6
Re: can you help me determine a winner to my program?
Posted 03 October 2011 - 10:10 PM
thanks for all of you .. ill try it ..
#7
Re: can you help me determine a winner to my program?
Posted 03 October 2011 - 10:20 PM
Glad we could help 
I should write a Snippet about it, asked so many times...
I should write a Snippet about it, asked so many times...
#9
Re: can you help me determine a winner to my program?
Posted 04 October 2011 - 03:25 AM
if (a.getSource()==button3)
{
if (buttons[0]==buttons[1]&&buttons[1]==buttons[2])
{
win=true;
}
}
This code is working but it doesnt show the winner .. here's my code for declaring the game is over ..
if(win == true){
JOptionPane.showMessageDialog(null, letter + " wins the game!");
JOptionPane.showInputDialog("Do you want to play again? Y/N");
char choice = 0;
if (choice=='Y'||choice=='y')
{
new JavaApplication11();
}
else if (choice=='N'||choice=='n')
{
JOptionPane.showMessageDialog(null,"GOODBYE!");
System.exit(0);
}
} else if(count == (boardSize*boardSize) && win == false){
JOptionPane.showMessageDialog(null, "The game was tie!");
}
}
i hope you can help me .. because its still dont show who's the winner ?
pbl, on 03 October 2011 - 10:20 PM, said:
Glad we could help 
I should write a Snippet about it, asked so many times...
I should write a Snippet about it, asked so many times...
Sir , i would to ask if where should i put your codes into my program .. im a bit confuse of where should i put it .. Hope you can help me again ? thanks ..
#10
Re: can you help me determine a winner to my program?
Posted 04 October 2011 - 08:16 AM
You will want to compare the text of those JButtons. See the JButton getText() method and the String equals() method. You don't want to compare the JButtons.
#11
Re: can you help me determine a winner to my program?
Posted 04 October 2011 - 01:50 PM
Your program is a little bit more complex than the one pbl posted.
Your starts with buttons that allow to select the size of your grid.
You will have to do something like that
Now, in the actuionPerformed you will also have to check for 10 * size and 100 * size instead of the harcoded value of 30 and 300
Your starts with buttons that allow to select the size of your grid.
You will have to do something like that
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JFrame implements ActionListener {
JButton three, five, seven;
TicTacToe() {
super("TicTacToe");
// a Panel for the buttons
JPanel p = new JPanel();
three = new JButton("3X3");
five = new JButton("5X5");
seven = new JButton("7X7");
p.add(three);
p.add(five);
p.add(seven);
three.addActionListener(this);
five.addActionListener(this);
seven.addActionListener(this);
add(p,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e) {
int size;
Object o = e.getSource();
if(o == three)
size = 3;
else if(o == five)
size = 5;
else
size = 7;
add(new BoardPanel(size));
}
class BoardPanel extends JPanel implements ActionListener {
TTTButton[][] button;
BoardPanel(int size) { // you will have to pass the size as parameter
super(new GridLayout(size,size);
button = new TTTButton[size][size];
for(int i = 0; i < size; ++i) {
for(int j = 0; j < size; ++j) {
button[i][j] = new TTTButton();
button[i][j].addActionListener(this);
add(button[i][j]);
}
}
}
}
Now, in the actuionPerformed you will also have to check for 10 * size and 100 * size instead of the harcoded value of 30 and 300
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote








|