mick18 0 Newbie Poster

Hi just wondering could anyone help me with this tic tac toe game i am developing it must be a GUI and include AI for the computer i originally made this tic tac toe game without the GUI but i am just having problems converting it to a GUI as it is my first attempt at a GUI i need help making the 'X' and 'O' appear when the button is clicked and i also believe my AI is no longer working since i converted it over any help much appreciated thanks!

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JOptionPane;
public class tictactoe 
{

	public static void main(String[]args)
	{
		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");
		}
	}
	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;		
	}
	
	/**
	 * Method used to calculate computer's move
	 */
	public static int compMove(char board[],char token)
	{
		//array of positions from the most wanted to the least desired
		int precedence[] = {4,0,2,6,8,1,3,5,7};		
		
		//runs twice, first to check if computer can complete a line, 
		//if not computer checks if it can block the player
		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
			
			//diagonal line '\'
			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;
			
			//diagonal line '/'
			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;
			
			//change over to check if computer needs to block player's potential lines
			if (token == 'O')
				token = 'X';
			else
				token = 'O';
		} //end outer for
		
		//if no line can be completed or blocked computer goes by the precedence array
		for (int i = 0; i < 9; i++) 
			if (board[precedence[i]] == ' ')
				return precedence[i];
		
		//all the positions are busy (a tie)
		return -1; 
	}
	
	/**
	 * Method used to output the final score and draw the board in its final state
	 */
	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;
		}		
	}
	
	/**
	 * The main method where program is executed
	 */
	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

} //end class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.