0 Replies - 181 Views - Last Post: 06 March 2013 - 07:45 PM Rate Topic: -----

#1 WeLikeAich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-March 13

Text Based Tic-Tac-Toe using recursion--Check up diagonal

Posted 06 March 2013 - 07:45 PM

Hi everyone, first time user. What I am trying to do is make a text based tic tac toe program without a GUI (I don't understand GUIs yet). I have two classes and a main method.

My main method plays the game going back and forth between playerX and playerO which are instances of the "Player" class I wrote. After a game is determined, it adds a win and/or loss to the respective players, displays the current score, and prompts for a decision to quit.


import java.util.Scanner;

public class Game
{
	public static void main (String args[])
	{
		Scanner scan = new Scanner(System.in);
		
		int turnCount = 2;
		boolean play = false;
		
		TicTacToeGrid game = new TicTacToeGrid();
		Player playerX = new Player();
		Player playerO = new Player();
		
		while (!play)
		{
			System.out.println(game);
			 
			
			
			if (turnCount % 2 == 0)
			{
				int position = scan.nextInt();
				game.placeX(position);
				//game.checkWin("X");
			}
			else
			{
				int position = scan.nextInt();
				game.placeO(position);
				//game.checkWin("O");
			}
			
			turnCount++;
			
			if (game.checkWin("X") == true || game.checkWin("O") == true)
			{
				if ((turnCount-1) % 2 == 0)
				{
					System.out.println("Player X has Won");
					playerX.addWin(1);
					playerO.addLoss(1);
				}
				else
				{
					System.out.println("Player O has Won");
					playerX.addLoss(1);
					playerO.addWin(1);
				}
				
				System.out.println("\n\nPlayer X \t Wins: " + "\t " + playerX.returnWin() + "\n \t \t Losses: " + playerX.returnLoss() + "\n");
				System.out.println("\n\nPlayer O \t Wins: " + "\t " + playerO.returnWin() + "\n \t \t Losses: " + playerO.returnLoss() + "\n");
								
				System.out.println("Would You Like to Quit? Yes or No");
				
				String quit = scan.nextLine();
				quit = scan.nextLine();
				
				if (quit.equalsIgnoreCase("Yes"))
				{
					play = true;
				}
				else
				{
					game = new TicTacToeGrid();
					turnCount = 2;
				}
				
			}
			
		}
		scan.close();
		
	}
}




My player class has two accessors, one for wins and one for losses, and two mutators, one for wins and one for losses.
(No problems here.)



public class Player 
{
	private int winCount;
	private int lossCount;
	
	/**
	 * Constructor for Player
	 */
	public Player()
	{
		winCount = 0;
		lossCount = 0;
	}
	
	/**
	 * adds a win to player
	 * @param newCount win increment
	 */
	public void addWin(int newCount)
	{
		winCount += newCount;
	}
	
	/**
	 * adds a Loss to player
	 * @param newCount	increment loss
	 */
	public void addLoss(int newCount)
	{
		lossCount += newCount;
	}
	
	/**
	 * returns win count
	 * @return amount of wins
	 */
	public int returnWin()
	{
		return winCount;
	}
	
	/**
	 * returns loss count
	 * @return	amount of losses
	 */
	public int returnLoss()
	{
		return lossCount;
	}
}




I have a TicTacToeGrid class that prints out the grid using an overridden "toString()" method, and checks to see if the grid has a match using recursion. It contains methods for placing X's and O's, using the position displayed in the grid(1-9). I check the columns, I check the rows, I check the Diagonal up, and the Diagonal down, each a different recursive method, which returns a boolean true if the row, column, or diagonal is the same string ("X" or "O"), or false if there is no match. I have a checkWin method which is iterative, which checks if there has been a winner by calling all of the directional checks, and returns a true if any of the checks are successful, representing a win.



My problem is with the check diagonal up method or "digUp". The game plays almost correctly. It checks the rows correctly, the columns correctly, and the Diagonal down correctly, but still returns a boolean of false when checking the up diagonal. Where I am getting lost is if the problem lies in my "digUp()" method or in my "checkWin" method, and on top of that I do not quite understand what the exact problem might be.


public class TicTacToeGrid
{
	private String[][] board;	
	
	/**
	 * Constructor for TicTacToe Grid
	 */
	public TicTacToeGrid()
	{
		board = new String [3][3];
		
		//initialize all the spots on the grid as blank spaces
		for (int r = 0; r < board.length; r++)
		{
			for (int c = 0; c < board.length; c++)
			{
				board[r][c] = ((Integer) (r*board.length+c+1)).toString();
			}
		}
	}
	
	/**
	 * 
	 * @param r index for row position
	 * @param c index for column position
	 * @param piece  x or o being placed
	 */
	public void place(int pos, String piece)
	{
		int c = (pos + 2) % (board.length);
		int r = (pos-c-1)/(board.length);
		board[r][c] = piece;
	}
	
	/**
	 * 		Places X
	 * @param newPos position to place X
	 */
	public void placeX(int newPos)
	{
		place(newPos, "X");
	}
	
	/**
	 * 		Places O
	 * @param newPos position to place O
	 */
	public void placeO(int newPos)
	{
		place(newPos, "O");
	}
	
	/**
	 * 
	 * @param r index for row position
	 * @param c index for column position
	 * @param piece  X or O, piece being checked for
	 */
	public boolean checkRow(int r, int c, String piece)
	{
			if (c == board.length-1)
			{
				return board[r][c].equals(piece);
			}
			else
			{
				return board[r][c].equals(piece) && checkRow(r, c+1, piece);
			}
	}
	
	/**
	 * 
	 * @param r index for row position
	 * @param c index for column position
	 * @param piece  X or O, piece being checked for
	 */
	public boolean checkColumn(int r, int c, String piece)
	{
		if (r == board.length-1)
		{
			return board[r][c].equals(piece);
		}
		else
		{
			return board[r][c].equals(piece) && checkColumn(r+1, c, piece);
		}
	}
	
	/**
	 * 
	 * @param r index for row position
	 * @param c index for column position
	 * @param piece  X or O, piece being checked for
	 */
	public boolean digDwn(int r, int c, String piece)
	{
		if(r == board.length-1)
		{
			return board[r][c].equals(piece);
		}
		
		else
		{
			return board[r][c].equals(piece) && digDwn(r+1, c+1, piece);
		}
		
	}
	
	/**
	 * 
	 * @param r index for row position
	 * @param c index for column position
	 * @param piece  X or O, piece being checked for
	 */
	public boolean digUp(int r, int c, String piece)
	{
		if(c == board.length-1 && r == 0)
		{
			return board[r][c].equals(piece);
		}
		
		else
		{
			return board[r][c].equals(piece) && digDwn(r-1, c+1, piece);
		}
	}
	
	/**
	 * 
	 * @param 	piece X or O, checking if player won
	 * @return	returns boolean true for win
	 */
	public boolean checkWin(String piece)
	{
		
		for (int i = 0; i < board.length; i++)
		{
			if (checkRow(i, 0, piece))
			{
				return true;
			}
			else
			
			if (checkColumn(0, i, piece))
			{
				return true;
			}
		}
		
		return digDwn(0, 0, piece) || digUp(board.length-1, 0, piece);
	}

	
	/**
	 * 		Prints out Board
	 */
	public String toString()
	{
		String printer = "";
		for (int r=0; r < board.length; r++)
		{
			printer += "       |       |       \n";
			for (int c = 0; c < board.length; c++)
			{
				printer += "   " + board[r][c] + "  ";
				if (c < board.length-1)
				{
					printer += " |";
				}
			}
			printer += "\n       |       |       \n";
			
			if (r < board.length-1)
				printer += "-------+-------+-------\n";
			
		}
		return printer;
	}
}





Many thanks,

WeLikeAich


P.S. I am aware of the other problem in which the letter does not print upon a winning placement. That is not my issue at the moment, i'm just concerned about the inability to check the up diagonal.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1