Tic Tac Toe Game

Do not know how to write to file and call it

Page 1 of 1

1 Replies - 1329 Views - Last Post: 03 December 2008 - 09:18 PM Rate Topic: -----

#1 isuckatjava  Icon User is offline

  • New D.I.C Head

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

Tic Tac Toe Game

Posted 03 December 2008 - 05:20 PM

i am having problems with getting my project for Tic Tac Toe i do not know how to write to a file and call that file and get the math to work. I have attached the file that shows the problem. the directions and my code is below

Project Phase II Description
For Phase II, you will save the results of the TicTacToe game to text file. Then you will retrieve these results and perform a statistical analysis on the results. The following are the detailed requirements:
Only the BoardManager will need to be modified;
Use the File, FileWriter, and PrintWriter classes to generate the output file and use the Scanner class to read the file (10 points);

Play 10000 games and write to a text file for each game, the winner of the game or write to the file, that there was a tie; example of the file content (10 points):
O Has Won
X Has Won
Board is Full -- No Winner
O Has Won
O Has Won

Add code to randomly select the player that makes the first selection. Your code should look like this (5 points):
SecureRandom first = new SecureRandom();
double O_or_X = first.nextDouble();
if (O_or_X < 0.5)
{
O selects first
Else
X selects first
}
Read the contents of your saved file and calculate the percent that O won, X won or there was a tie. Your only output should like this (10 points):
Percent O Wins: 46.87
Percent X Wins: 47.11
Percent tie: 6.02






import java.security.SecureRandom;
import javax.swing.JOptionPane;
public class BoardManager {
	// instance variables and object references
	// declared below:
	private Board gameBoard = null;
	private Player xPlayer = null;
	private Player oPlayer = null;

	private boolean isBoardFull = false;
	private boolean hasXWon = false;
	private boolean hasOWon = false;

	public static final char BLANK = 'b';
	public static final char O = 'O';
	public static final char X = 'X';
	public static final int ROWS = 3;
	public static final int COLUMNS = 3;

	public final int number_of_sims = 10000;

	// no argument constructor
	public BoardManager()
	{
		// create the the Board object --
		// see Board constructor for required args
		gameBoard = new Board(ROWS, COLUMNS, BLANK);
		// create a X and an O player
		xPlayer = new Player();
		oPlayer = new Player();
	}

	public static void main(String[] args)
	{
		BoardManager boardmanager = new BoardManager();
		boardmanager.play();
	}

	// need to complete this method
	public void initTheGame()
	{
		// initTheBoard
		// Hint: you need to use a gameBoard
		// method to do this
		gameBoard.initTheBoard();

	}

	// need to complete this method
	public void play()
	{
		// you need to initialize the game;
		// you have a method to do this
		initTheGame();

		// create an array to hold the players board selection
		SecureRandom first = new SecureRandom();
		double O_or_X = first.nextDouble();
		if (O_or_X < 0.5)
		{
			O selects first}
			else;
		{
			X selects first;
		}

		// loop until 1 of 3 things happens:
		// 1. O wins
		// 2. X wins
		// 3. The board is full -- a tie
		while (!hasOWon && !hasXWon && !isBoardFull)
		{
			boolean b = false;
			while (!b)
			{
				// ask o to select a position
				// check if position is open
				// break if position is open
				b = gameBoard.set_APosition(oPlayer.request_Board_Position(), O);
			}

			// check to see if the game is finished
			// if so, break out of the loop
			hasXWon();hasOWon();isBoardFull();
			if(hasXWon || hasOWon || isBoardFull) break;

			b = false;
			while (!b)
			{
				// ask x to select a position

				// check if position is open
				// break if position is open
				b = gameBoard.set_APosition(xPlayer.request_Board_Position(), X);
			}
			// check to see if the game is finished
			// if so, break out of the loop
			hasXWon();hasOWon();isBoardFull();
			if(hasXWon || hasOWon || isBoardFull) break;
		}

		// show results of the game;
		// you have a method to do this
		showResults();
	}

	// determine if the board is full
	public boolean isBoardFull()
	{
		// you need access to the board array;
		// the gameBoard reference has a method
		// that can return the board array
		// Hint: you will probably want to create
		// a local copy of the the game board array
		// to examine the contents.
		// !!!!Remember to setBoardIsFull to true if it is!!

		// call setBoardIsFull
		// set board_is_full
		char[][] boardCopy = gameBoard.getTheBoard();
		boolean r = true;
		for(int a = 0; a < boardCopy.length; a++)
			for(int b = 0; b < boardCopy.length; b++)
				if(boardCopy[a][b] == BLANK)
					r = false;
		isBoardFull = r;
		return r;
	}

	// determine if O has won
	public boolean hasOWon()
	{
		// you need access to the board array
		// the gameBoard reference has a method
		// that can return the board array
		// Hint: you will probably want to create
		// a local copy of the the game board array
		// to examine the contents.
		// !!!!Remember to setOHasWon to true if O won !!

		// call setOHasWon
		// set O_has_won
		char[][] boardCopy = gameBoard.getTheBoard();
		//horizontal
		boolean b1 = true;
		for(int a = 0; a < boardCopy.length; a++){
			b1 = true;
			for(int c = 0; c < boardCopy[a].length; c++){
				if(boardCopy[a][c] != O)
					b1 = false;
			}
			if(b1)break;
		}
		//vertical
		boolean b2 = true;
		for(int c = 0; c < boardCopy.length; c++){
			b2 = true;
			for(int a = 0; a < boardCopy[c].length; a++){
				if(boardCopy[a][c] != O)
					b2 = false;
			}
			if(b2)break;
		}
		//diagonal
		int a = 0;
		boolean b3 = true;
		while(a < boardCopy.length){
			if(boardCopy[a][a] != X)
				b3 = false;
			a++;
		}
		boolean b4 = true;
		a = boardCopy.length - 1;
		while(a >= 0){
			if(boardCopy[a][a] != X)
				b4 = false;
			a--;
		}
		hasOWon = b1 || b2 || b3 || b4;
		return hasOWon;
	}

	// determine if X won
	public boolean hasXWon()
	{
//		you need access to the board array
		// the gameBoard reference has a method
		// that can return the board array
		// Hint: you will probably want to create
		// a local copy of the the game board array
		// to examine the contents.
		// !!!!Remember to setOHasWon to true if O won !!

		// call setOHasWon
		// set O_has_won
		char[][] boardCopy = gameBoard.getTheBoard();
		//horizontal
		boolean b1 = true;
		for(int a = 0; a < boardCopy.length; a++){
			b1 = true;
			for(int c = 0; c < boardCopy[a].length; c++){
				if(boardCopy[a][c] != X)
					b1 = false;
			}
			if(b1)break;
		}
		//vertical
		boolean b2 = true;
		for(int c = 0; c < boardCopy.length; c++){
			b2 = true;
			for(int a = 0; a < boardCopy[c].length; a++){
				if(boardCopy[a][c] != X)
					b2 = false;
			}
			if(b2)break;
		}
		//diagonal
		int a = 0;
		boolean b3 = true;
		while(a < boardCopy.length){
			if(boardCopy[a][a] != X)
				b3 = false;
			a++;
		}
		boolean b4 = true;
		a = boardCopy.length - 1;
		while(a >= 0){
			if(boardCopy[a][a] != X)
				b4 = false;
			a--;
		}
		hasXWon = b1 || b2 || b3 || b4;
		return hasXWon;
	}

	// show the results of the game
	public void showResults()
	{
		String results;
		if(hasOWon)
			results = "O has won the game!";
		else if(hasXWon)
			results = "X has won the game!";
		else
			results = "It's a tie!";


		// set the result String to identify the
		// the results of the game

		// you need access to the board array
		// the gameBoard reference has a method
		// that can return the board array
		// Hint: you will probably want to create
		// a local copy of the the game board array
		// to examine the contents.
		char[][] theBoard = gameBoard.getTheBoard();


		//display the contents of the board
		// Hint: you can use 2 for-loops (nested) to
		// do this
		int i, j;

		for (i = 0; i < theBoard.length; i++)
		{
			for (j=0; j < theBoard[i].length; j++)
				System.out.print(theBoard[i][j] + " ");
			System.out.println();
		}
		JOptionPane.showMessageDialog(null, results, "Results of the Game",
				JOptionPane.INFORMATION_MESSAGE);

	}
}


This post has been edited by isuckatjava: 03 December 2008 - 05:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe Game

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Tic Tac Toe Game

Posted 03 December 2008 - 09:18 PM

Before you need to worry about file handling you should get the program to actually play a game.
...
BoardManager boardmanager = new BoardManager();
boardmanager.play();
...



Where is the boardmanager.play() method coded?

This post has been edited by n8wxs: 03 December 2008 - 09:18 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1