2 Replies - 3780 Views - Last Post: 23 April 2010 - 06:13 PM Rate Topic: -----

#1 TheRaiderNation  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 81
  • Joined: 11-January 10

Tic-Tac-Toe Game

Posted 23 April 2010 - 06:01 PM

Alright so I've been writing a simple tic-tac-toe game. The multiplayer works perfectly fine, the problem I'm getting is that when I try to run the single player mode the program still asks for player 2 to give input instead of the computer automatically doing it.

Code:

Tic-Tac-Toe Class
import chn.util.*;
 
public class TicTacToe 
{
	ConsoleIO myConsole;
	String player1;
	String player2;
	String winner;
	int turns;
	boolean gameOver;
	boolean singlePlayer;
	int[] spaces;
	
	public TicTacToe()
	{
		player1 = null;
		player2 = null;
		winner = "noone";
		turns = 0;
		singlePlayer = false;
		gameOver = false;
		myConsole = new ConsoleIO();
		spaces = new int[9];//0-space open 1-occupied by 1 2-occupied by 2
	}
	 
	public void instructions()
	{
		System.out.println("---Instructions---");
		System.out.println("When prompted, enter the location you would like to");
		System.out.println("place your token (either X or O). To enter which   ");
		System.out.println("space you want, enter the a digit 1-9 (must be an empty");
		System.out.println("space to place token. First player to have 3 in a row");
		System.out.println("wins.");
		System.out.println("Player 1-----> X");
		System.out.println("Player 2-----> O");
		System.out.println("Note: In Single player games the computer is always O");
		System.out.println("");
	}
	
	public void setUp()
	{
		int singMult = 0;
		System.out.print("(1) Single Player \n(2) Multiplayer");
		System.out.println("");
		System.out.print("Which game mode? ");
		singMult = myConsole.readInt();
		if(singMult==1)
			singPlayer();
		else
			multPlayer();
	}
	
	public void singPlayer()
	{
		singlePlayer=true;
		System.out.println("-------------------");
		System.out.println("Enter your name: ");
		System.out.print("Player-----> ");
		player1 = myConsole.readLine();
		player2 = "Computer";
		boardSetUp();
		runSingPlayer();
	}
	
	public void multPlayer()
	{
		singlePlayer=false;
		System.out.println("-------------------");
		System.out.println("Enter player names: ");
		System.out.print("Player 1-----> ");
		player1 = myConsole.readLine();
		System.out.print("Player 2-----> ");
		player2 = myConsole.readLine();
		boardSetUp();
		runMultPlayer();
	}
	
	public void boardSetUp()
	{
		for(int i=0;i<spaces.length;i++)
		{
			spaces[i]=0;
		}
	}
	
	public void gameBoard()
	{
		int spaceNum=0;
		
		for(int i=0;i<spaces.length;i++)
		{
			spaceNum++;
			if(spaceNum%3==1)
			{
				System.out.println();
			}
			
			if(spaces[i]==0)
			{
				System.out.print(spaceNum + " ");
			}
			else if(spaces[i]==1)
				{
					System.out.print("X ");
				}
				else if(spaces[i]==2)
					{
						System.out.print("O ");
					}
			
		}
		System.out.println();
	}  
	
	public void player1Move()
	{
		boolean moveNotValid = true;
		int moveLoc = -1;
		
		while(moveNotValid == true)
		{
			System.out.print("Player 1 move: ");
			moveLoc = myConsole.readInt();
	
			if(spaces[moveLoc-1]==0)
			{
				spaces[moveLoc-1]=1;
				moveNotValid = false;
			}
			else
			{
				System.out.println("Space taken by " + player2);
			}
		}
	}
	
	public void player2Move()
	{
		boolean moveNotValid = true;
		int moveLoc = -1;
		
		while(moveNotValid == true)
		{
			System.out.print("Player 2 move: ");
			moveLoc = myConsole.readInt();
	
			if(spaces[moveLoc-1]==0)
			{
				spaces[moveLoc-1]=2;
				moveNotValid = false;
			}
			else
			{
				System.out.println("Space taken by " + player1);
			}
		}
	}
	
	public void cpuMove()
	{
		
		computer cp = new computer(spaces);
		boolean moveNotValid = true;
		int moveLoc = -1;
		
		while(moveNotValid == true)
		{
			moveLoc = cp.makeMove();
	
			if(spaces[moveLoc-1]==0)
			{
				spaces[moveLoc-1]=2;
				moveNotValid = false;
			}
			else
			{
				System.out.println("Space taken by " + player1);
			}
		}
	}
	
	public void checkForWin()
	{
		//Preform 8 checks for both players & cats game check (total 17 checks)
		//Player 1 win conditions
		if(spaces[0]==1 && spaces[4]==1 && spaces[8]==1)
		{
			//*--
			//-*-
			//--*
			winner = player1;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[2]==1 && spaces[4]==1 && spaces[6]==1)
		{
			//--*
			//-*-
			//*--
			winner = player1;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[0]==1 && spaces[3]==1 && spaces[6]==1)
		{
			//*--
			//*--
			//*--
			winner = player1;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[1]==1 && spaces[4]==1 && spaces[7]==1)
		{
			//-*-
			//-*-
			//-*-
			winner = player1;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[2]==1 && spaces[5]==1 && spaces[8]==1)
		{
			//--*
			//--*
			//--*
			winner = player1;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[0]==1 && spaces[1]==1 && spaces[2]==1)
		{
			//***
			//---
			//---
			winner = player1;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[3]==1 && spaces[4]==1 && spaces[5]==1)
		{
			//---
			//***
			//---
			winner = player1;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[6]==1 && spaces[7]==1 && spaces[8]==1)
		{
			//---
			//---
			//***
			winner = player1;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		//Player 2 win conditions
		if(spaces[0]==2 && spaces[4]==2 && spaces[8]==2)
		{
			//*--
			//-*-
			//--*
			winner = player1;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[2]==2 && spaces[4]==2 && spaces[6]==2)
		{
			//--*
			//-*-
			//*--
			winner = player1;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[0]==2 && spaces[3]==2 && spaces[6]==2)
		{
			//*--
			//*--
			//*--
			winner = player2;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[1]==2 && spaces[4]==2 && spaces[7]==2)
		{
			//-*-
			//-*-
			//-*-
			winner = player2;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[2]==2 && spaces[5]==2 && spaces[8]==2)
		{
			//--*
			//--*
			//--*
			winner = player2;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[0]==2 && spaces[1]==2 && spaces[2]==2)
		{
			//***
			//---
			//---
			winner = player2;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[3]==2 && spaces[4]==2 && spaces[5]==2)
		{
			//---
			//***
			//---
			winner = player2;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		if(spaces[6]==2 && spaces[7]==2 && spaces[8]==2)
		{
			//---
			//---
			//***
			winner = player2;
			gameOver = true;
			System.out.println("The winner is " + winner);
		}
		
		//Tie Game Check
		if(turns==9 && winner.equals("noone"))
		{
			System.out.println("It's a cats game!");
			gameOver = true;
		}
		
		if(winner.equals(player1))
		{
			gameBoard();
		}
		else if(winner.equals(player2))
			{
				gameBoard();
			}
	}
	
	public void runSingPlayer()
	{
		boolean moveNotValid = true;
		int playerTurn = 1;
		
		while(gameOver == false)
		{	
			gameBoard();
			if(playerTurn==1)
			{
				player1Move();
				playerTurn = 2;
				turns++;
			}
			else
			{
				player2Move();
				playerTurn = 1;
				turns++;
			}
			checkForWin();
		}
		playAgain();
		
	}
	
	public void runMultPlayer()
	{
		boolean moveNotValid = true;
		int playerTurn = 1;
		
		while(gameOver == false)
		{	
			gameBoard();
			if(playerTurn==1)
			{
				player1Move();
				playerTurn = 2;
				turns++;
			}
			else
			{
				cpuMove();
				playerTurn = 1;
				turns++;
			}
			checkForWin();
		}
		playAgain();	
	}
	
	public void playAgain()
	{
		String answer = null;
		System.out.print("Play again?(Y/N) ");
		answer = myConsole.readLine();
		if(answer.equalsIgnoreCase("Y"))
		{
			gameOver = false;
			boardSetUp();
			if(singlePlayer == true)
				runSingPlayer();
			else
				runMultPlayer();
		}
	}
	
    public static void main(String[] args) 
    {
    	TicTacToe game = new TicTacToe();
    	
    	game.instructions();
    	game.setUp();
    }
}


computer Class
//Artificial Intelligence

class computer 
{
	int[] spaces;
	
	public computer(int[] array)
	{
		spaces = new int[array.length];
		for(int i=0;i<array.length;i++)
		{
			spaces[i] = array[i];
		}
		//later on implement difficulty
		/*Idea:
		 *Extremely Hard- 5% Error rate
		 *Hard-10% Error rate
		 *Medium-60% Error rate
		 *Easy- 80% Error rate
		 *Super Easy- 90% Error rate
		 */
	}
	
	public int makeMove()
	{
		int move=0;
		boolean makingMove = true;
		//Step 1) Read in board
		//Step 2) Read in move Locations
		//Step 3) Calculate best move(Currently done as random move)
		//Step 4) Place token
		
		while(makingMove == true)
		{
			double rand = Math.random();
			if(rand>.111 && spaces[0]==0)
			{
				makingMove=false;
				move=1;
			}
			
			if(rand>.222 && spaces[1]==0)
			{
				makingMove=false;
				move=2;
			}
			if(rand>.333 && spaces[2]==0)
			{
				makingMove=false;
				move=3;
			}
			if(rand>.444 && spaces[3]==0)
			{
				makingMove=false;
				move=4;
			}
			if(rand>.555 && spaces[4]==4)
			{
				makingMove=false;
				move=5;;
			}
			if(rand>.666 && spaces[5]==0)
			{
				makingMove=false;
				move=6;
			}
			if(rand>.777 && spaces[6]==0)
			{
				makingMove=false;
				move=7;
			}
			if(rand>.888 && spaces[7]==0)
			{
				makingMove=false;
				move=8;
			}
			if(rand>=1 && spaces[8]==0)
			{
				makingMove=false;
				move=9;
			}
		}
		return move;
	}
}



Also, any suggestions on how to make the code more efficient would be appreciated, I realize its a bit cumbersome with the amount of lines that are in the tictactoe class alone. Do you think making a checker class for the checkForWin method would be a good idea?
Thanks in advance.

This post has been edited by TheRaiderNation: 23 April 2010 - 06:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Tic-Tac-Toe Game

#2 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 118
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: Tic-Tac-Toe Game

Posted 23 April 2010 - 06:07 PM

public void runSingPlayer()
        {
                boolean moveNotValid = true;
                int playerTurn = 1;
                
                while(gameOver == false)
                {       
                        gameBoard();
                        if(playerTurn==1)
                        {
                                player1Move();
                                playerTurn = 2;
                                turns++;
                        }
                        else
                        {
                                player2Move();
                                playerTurn = 1;
                                turns++;
                        }
                        checkForWin();
                }
                playAgain();
                
        }


You swapped the two methods, but here is some advice, make one method that you pass a boolean to, singPlayer. If it is true have you program call cpu, other wise have to call player two

Also your player1Move, and player2Move can be condensed into one, by passing a String, and other needed data to it

This post has been edited by ianian112: 23 April 2010 - 06:13 PM

Was This Post Helpful? 1
  • +
  • -

#3 TheRaiderNation  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 81
  • Joined: 11-January 10

Re: Tic-Tac-Toe Game

Posted 23 April 2010 - 06:13 PM

*facepalm* your right. I got the multPlayer and singPlayer methods mixed up lol. Thanks though
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1