package tictac;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Main {
tictactoeGUI game = new tictactoeGUI("Tic Tac Toe");
}
class tictactoeGUI extends JFrame implements ActionListener
{
private JButton[] button = new JButton[9];
private JButton reset = new JButton ("Reset");
private JButton quit = new JButton ("Quit");
private Container content;
private JPanel buttongrid = new JPanel();
private JPanel extraP = new JPanel();
Font font = new Font("Dialog", Font.BOLD, 16);
public tictactoeGUI (String str)
{
super(str);
content = getContentPane();
content.setLayout(new BorderLayout());
buttongrid.setLayout(new GridLayout(3,3));
for(int i=0; i<button.length; i ++)
{
button[i] = new JButton("");
button[i].setActionCommand(Integer.toString(i));
button[i].addActionListener(this);
buttongrid.add(button[i]);
}
extraP.setLayout(new GridLayout(1,2));
extraP.add(reset); reset.addActionListener(this);
extraP.add(quit); quit.addActionListener(this);
content.add(buttongrid, BorderLayout.CENTER);
content.add(extraP, BorderLayout.SOUTH);
setSize(200,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object target = e.getSource();
int grid_Pos = Integer.parseInt(e.getActionCommand());
System.out.println("Grid button " +grid_Pos+" was pressed");
if(target==reset)
{
System.out.println("Grid button " +grid_Pos+" was pressed");
}
}
}
------------------------------------------------------------------------------------------------------------------
package tictac;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JOptionPane;
public class MyGame {
public static void main(String[] args)
{
do // do-while loop used to let user run the program multiple times
{
//variables declaration
char board[] = new char[9]; //logical board
for (int i = 0; i < 9; i++)
board[i] = ' '; //logical board filled with initial 'empty' values
int cell; //used to read player's input
int compCell; //computer's chosen cell
int option; //used to determine player's chosen option at the beginning:
//0 = Yes, 1 = No, 2 = Cancel
char playToken = 'X'; //used to determine player's token, 'X' or 'O'
char compToken = 'O'; //used to determine computer's token
boolean endBool = false; //used to determine if the game has been resolved
//welcome message and option if player wants to go first
option = JOptionPane.showConfirmDialog(null,"Welcome to \"Tic Tac Toe\"!" +
"\nWould you like to go first?");
if (option == 0) //player chose 'yes'
{
playToken = 'X'; //player chose to go first, his token is 'X'
compToken = 'O'; //resolves computer's token based on the player's one
}
else if (option == 1) //player chose 'no'
{
playToken = 'O'; //player chose to go second, his token is 'O'
compToken = 'X'; //resolves computer's token based on the player's one
board[compMove(board,compToken)] = compToken; //stores the input of computer's move
}
else //player chose 'cancel', option = 2
System.exit(0); //terminates the program
System.out.println("***************");
//main loop, the core of the program
while (endBool == false)
{
if (checkWin(board, playToken)) //checks if player has made a line
{
endBool = true; //exits the while loop
showScore(1,board); //shows the score, game is over
} //end if
else
{
compCell = compMove(board,compToken); //computer's move
if (compCell == -1)
{
endBool = true; //exits the while loop
showScore(0,board); //shows the score, game is over
break;
}
board[compCell] = compToken; //stores the input of computer's move
//print out the message informing the player of computer's move
System.out.println("Computer has put his token on the positon number: "
+ compCell);
if (checkWin(board, compToken)) //checks if computer has made a line
{
endBool = true; //exits the while loop
showScore(2,board); //shows the score, game is over
} //end if
else
{
endBool = true;
//checks if there is an empty cell
for (int i = 0; i < 9; i++)
if (board[i] == ' ')
endBool = false;
if (endBool) //if there is no empty cell, then it is a tie
{
showScore(0,board); //shows the score, game is over
}
else
System.out.println("Your turn.");
} //end else
} //end else
} //end while
//player has an option to rerun the game
} while (JOptionPane.showConfirmDialog(null, "Would You like to go again?") == 0);
} // end main method
public static void showScore(int score, char board[])
{
System.out.println("***************\n");
switch (score)
{
case 0: //tie
JOptionPane.showMessageDialog(null, "It is a tie!");
break;
case 1: //win
JOptionPane.showMessageDialog(null, "Congratulations! You have won!");
break;
case 2: //loose
JOptionPane.showMessageDialog(null, "Computer won! You loose!");
break;
}
}
public static boolean checkWin(char board[], char token)
{
// [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6] - win
for (int i = 0; i <= 2; i++)
{
//horizontal lines
if (board[i*3] == token && board[i*3+1] == token && board[i*3+2] == token) // ' X | X | X '
return true;
//vertical lines
else if (board[i] == token && board[i+3] == token && board[i+6] == token) // ' X - X - X '
return true;
}
//diagonal lines
if (board[0] == token && board[4] == token && board[8] == token) // ' X \ X \ X '
return true;
else if (board[2] == token && board[4] == token && board[6] == token) // ' X / X / X '
return true;
//no line
return false;
}
public static int compMove(char board[],char token)
{
int precedence[] = {4,0,2,6,8,1,3,5,7};
for (int twice = 1; twice <= 2; twice++)
{
for (int i = 0; i <= 2; i++) //used for vertical and horizontal lines check
{
//horizontal lines
if (board[i*3] == token && board[i*3+1] == token && board[i*3+2] == ' ') // ' X | X | _ '
return i*3+2;
else if (board[i*3] == token && board[i*3+1] == ' ' && board[i*3+2] == token) // ' X | _ | X '
return i*3+1;
else if (board[i*3] == ' ' && board[i*3+1] == token && board[i*3+2] == token) // ' _ | X | X '
return i*3;
//vertical lines
if (board[i] == token && board[i+3] == token && board[i+6] == ' ') // ' X - X - _ '
return i+6;
else if (board[i] == token && board[i+3] == ' ' && board[i+6] == token) // ' X - _ - X '
return i+3;
else if (board[i] == ' ' && board[i+3] == token && board[i+6] == token) // ' _ - X - X '
return i;
} //end inner for
if (board[0] == token && board[4] == token && board[8] == ' ') // ' X \ X \ _ '
return 8;
else if (board[0] == token && board[4] == ' ' && board[8] == token) // ' X \ _ \ X '
return 4;
else if (board[0] == ' ' && board[4] == token && board[8] == token) // ' _ \ X \ X '
return 0;
if (board[2] == token && board[4] == token && board[6] == ' ') // ' X / X / _ '
return 6;
else if (board[2] == token && board[4] == ' ' && board[6] == token) // ' X / _ / X '
return 4;
else if (board[2] == ' ' && board[4] == token && board[6] == token) // ' _ / X / X '
return 2;
if (token == 'O')
token = 'X';
else
token = 'O';
}
for (int i = 0; i < 9; i++)
if (board[precedence[i]] == ' ')
return precedence[i];
return -1;
}
}
TIC TAC TOE GAME using GUI......THE BOARD DOESNT APPEAR WHEN I RUN IT.
Started by evelina, Jun 02 2010 05:08 PM
3 replies to this topic
#1
Posted 02 June 2010 - 05:08 PM
THIS IS THE CODE I HAVE, WHEN I RUN IT THE TIC TAC TOE DOES NOT COME UP...CAN ANYONE HELP ME?
|
|
|
#2
Posted 02 June 2010 - 05:27 PM
I'm 100% positive there is a Java Tutorial on Tic Tac Toe. So if I can't help take a look at that.
And I'm not a Java Expert, but I am pretty sure that you need a JFrame and then you add the JPanel to the JFrame. Because a Panel isn't a Frame.
And I'm not a Java Expert, but I am pretty sure that you need a JFrame and then you add the JPanel to the JFrame. Because a Panel isn't a Frame.
#3
Posted 03 June 2010 - 03:55 AM
Because you don't create a tictactoeGui object?
#4
Posted 23 June 2010 - 12:03 AM
You should put
"tictactoeGUI game = new tictactoeGUI("Tic Tac Toe");"
command in the main method that you have in MyGame class. You dont'need the Main class you created.
You should adjust a couple of things, too:
- computer don't wait for user to do his move;
- buttons appearance doesn't change when user clicks on one of them.
"tictactoeGUI game = new tictactoeGUI("Tic Tac Toe");"
command in the main method that you have in MyGame class. You dont'need the Main class you created.
You should adjust a couple of things, too:
- computer don't wait for user to do his move;
- buttons appearance doesn't change when user clicks on one of them.


Sign In
Create Account

Back to top









