Java TicTacToe code, need getWinner method

please help me with a getWinner method

Page 1 of 1

3 Replies - 13571 Views - Last Post: 09 December 2008 - 05:31 PM Rate Topic: -----

#1 xavier_mark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-October 07

Java TicTacToe code, need getWinner method

Post icon  Posted 24 October 2007 - 06:01 PM

//I need help writing a method that will check for a winner, and return the winner. Thanks guys.

//this is the client code, see below for tester code
/**
  A 3 x 3 tic-tac-toe board.
*/
public class TicTacToe
{
  /**
	 Constructs an empty board.
  */
  public TicTacToe()
  {
	 board = new String[ROWS][COLUMNS];
	 // Fill with spaces
	 for (int i = 0; i < ROWS; i++)
		for (int j = 0; j < COLUMNS; j++)
		   board[i][j] = " ";
  }

  /**
	 Sets a field in the board. The field must be unoccupied.
	 @param i the row index
	 @param j the column index
	 @param player the player ("x" or "o")
  */
  public void set(int i, int j, String player)
  {
	 if (board[i][j].equals(" "))
		board[i][j] = player;
  }

  /**
	 Creates a string representation of the board, such as
	 |x  o|
	 |  x |
	 |   o|
	 @return the string representation
  */
  public String toString()
  {
	 String r = "";
	 for (int i = 0; i < ROWS; i++)
	 {
		r = r + "|";
		for (int j = 0; j < COLUMNS; j++)
		   r = r + board[i][j];
		r = r + "|\n";
	 }
	 return r;
  }

  public String getWinner()
  {
	  
  }
  private String[][] board;
  private static final int ROWS = 3;
  private static final int COLUMNS = 3;
}





//the following is the tester code
import java.util.Scanner;

/**
  This program runs a TicTacToe game. It prompts the
  user to set positions on the board and prints out the
  result.
*/
public class TicTacToeRunner
{
  public static void main(String[] args)
  {
	 Scanner in = new Scanner(System.in);
	 String player = "x";
	 TicTacToe game = new TicTacToe();
	 boolean done = false;
	 while (!done)
	 {
		System.out.print(game.toString());
		System.out.print(
			  "Row for " + player + " (-1 to exit): ");
		int row = in.nextInt();
		if (row < 0) done = true;
		else
		{
		   System.out.print("Column for " + player + ": ");
		   int column = in.nextInt();
		   game.set(row, column, player);
		   if (player.equals("x"))
			  player = "o";
		   else
			  player = "x";
		}
	 }
  }
}


Attached File  tictactoe_and_tictactoerunner.zip (1.15K)
Number of downloads: 472

Is This A Good Question/Topic? 0
  • +

Replies To: Java TicTacToe code, need getWinner method

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Java TicTacToe code, need getWinner method

Posted 25 October 2007 - 03:51 AM

well you can check who the winner is by iterating through the matrix... then decide the winner and return the winner as a string
Was This Post Helpful? 0
  • +
  • -

#3 wind851218  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-December 08

Re: Java TicTacToe code, need getWinner method

Posted 09 December 2008 - 05:07 PM

I got this for the getWinner code, however it has some errors that could not compile, can someone help me to check where i did wrong?
public String getWinner()
{
String winner = getWinner(0);
if (winner.equals(" "))
winner = getWinner(1);
if (winner.equals(" "))
winner = getWinner(2);
else
return " ";
}

/**
* Determines whether there is a winner in a given row
* @param aRow the row to check
* @return winner "x" or "o" in the given row, or " " if there is
* no winner in that row.
*/
private String getWinner(int aRow, int aColumn)
{
if (board[aRow][0].equals(board[aRow][1]) &&
board[aRow][0].equals(board[aRow][2]))
return board[aRow][0];
if (board[aColumn][0].equals(board[aColumn][1]) &&
board[aColumn][0].equals(board[aColumn][2]))
return board[aColumn][0];
else
return " ";
}
Was This Post Helpful? 0
  • +
  • -

#4 JBabineau  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 68
  • Joined: 05-December 08

Re: Java TicTacToe code, need getWinner method

Posted 09 December 2008 - 05:31 PM

From what I can tell is you call getWinner(int) from getWinner but you do not have a getWinner(int) method you have getWinner(int, int)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1