6 Replies - 6331 Views - Last Post: 19 November 2008 - 06:33 PM Rate Topic: -----

#1 safialan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-October 08

TicTacToe class --> not an applet

Post icon  Posted 17 November 2008 - 09:18 PM

Hi Guys,

I am completely lost with this TicTacToe game. I searched on the forum and all I could find was Tic Tac Toe games with an applet code. My assignment requires me to write a Java class called TicTacToe java.

1) 3 by 3 array - initialized with an '*' . Output the TicTacToe board. (That part I succeeded )
2) Prompt the 1st player to select a row
Promt the 1st user to select a column (I did that part, too)
3) Check if the location has been previously selected, if not change the value location from an '*' to an 'X'. If previously selected, inform the player and prompt the same player to select another location. (This part I couldn't really get!!)
4) Once player has selected a valid location and his marker has been placed, I need to check if the player has won.

Technical Specifications;
- create a class called TicTacToe
- must have a method called mPlay
- must have 4 other methods:
1) A void method mPopularCard (will accept the char array and populate with '*')
2) A void method mPrintCard (will accept char array and output it to the screen)
3) A boolean method mCatsGame (will accept the char array and return a value of true if the board is completely filled with markers and neither player has won and a value of false otherwise)
4) A boolean method called mDidIWin (will accept the chararray and the player number and will return a valur of true if the player has won and a value of false otherwise.)

I just don't know how to start, I am very new to Java... I couldn't even do the method mPlay, because it won't compile without the main method. This is so far I got. Could you please advice how I should go with this??


import java.util.Scanner;

public class TicTacToe {
	
	public static void main( String args[]) {
		
	  
	 { 
			char[][]board = new char [3][3];
			int counter = 0;
			int row =0;
			int column =0;
			int number;
			
			
		  System.out.println ("This program allows you to play Tic-Tac-Toe");  
		  System.out.println();

		  Scanner input = new Scanner( System.in ); 
					
		  for (int i =0; i<board.length; i++)
		 { 
			for(int j=0;j<board[i].length;j++)
			
			{
				System.out.print (" * ");
			
			}
			
	 		System.out.println();
		}
	   
		  System.out.println ();
		  System.out.println ("Player 1: Enter a row 1 - 3.");
		  row = input.nextInt();
		  System.out.println ("Player 1: Enter a column 1 - 3.");
		  column = input.nextInt();	  
	
}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: TicTacToe class --> not an applet

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4425
  • View blog
  • Posts: 12,293
  • Joined: 18-April 07

Re: TicTacToe class --> not an applet

Posted 17 November 2008 - 09:41 PM

With a project like this it is very beneficial to get the print method working as soon as possible. That way you can see the board and use it during your testing of the other functions.

So I would go about starting your print method where you are going to have nested for loops which will have the outer one loop through the rows and the other loop through the columns. Its goal is simple, display what is in the space at the coordinates row,column.

Now you have a nested for loop there that prints an asterisk, but that is not initializing the array. Be sure to place the characters into the board[3][3] array during each iteration.

// Initialize the board...

for (int i =0; i<board.length; i++)
{
     for(int j=0;j<board[i].length;j++)
     {
          // Set each element to the asterisk
          board[i][j] = '*'; 
     }  
}



Now you will have a similar setup for your print method, but this time you will read each value in board and print what is there...


// Print the board

for (int i =0; i<board.length; i++)
{
     for(int j=0;j<board[i].length;j++)
     {
          // Print each value in the board
          System.out.println(board[i][j] + " "); 
     }  
}



Once we have the board printable, we can then work on the other functions which will set tokens. We ask the player to enter a spot in the board and look it up. We then check if a character (other than *) is there and if it is, then we know that the spot is taken.

if (board[rownum][colnum] != '*') { 
     // We know this is taken by a player already
}
else { 
     // It is available, place the token at the coordinates rownum, colnum
}



So hopefully you get the idea of what you need to do now. To check for wins you will need to setup some loops that test the various directions for all the same character.

I think you can handle that part. Enjoy!

"At DIC we be tictactoe code ninjas... we also do donkey kong, frogger, and asteroids!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: TicTacToe class --> not an applet

Posted 18 November 2008 - 09:28 AM

Your tech specs are a little confusing. I say this because most every method "will accept the char array and". That would seem to be the point of having a class, to maintain the state of that "char array".

Here's my interpretation of your class, using the code you've already offered.

import java.util.Scanner;
// - create a class called TicTacToe
public class TicTacToe {
	// that "char array" defined as a private attibute of the object
	private final int ROWS = 3;
	private final int COLS = 3;
	private char[][]board = new char [ROWS][COLS];
	
	private Scanner input = new Scanner( System.in ); 

	public TicTacToe() {
		// when you first start, you'll what to clear the array values
		mPopulateCard();
	}
	
	// 1) A void method mPopularCard (will accept the char array and populate with '*')
	// I'm going to guess this is supposed to be mPopulateCard
	public void mPopulateCard() {
		// needs work
		// use the two loops you've already written
		// look at mPrintCard() for some ideas
	}
	
	
	//2) A void method mPrintCard (will accept char array and output it to the screen)
	public void mPrintCard() {
		// using code you've already offered
		for (int i=0; i<board.length; i++) { 
			for(int j=0; j<board[i].length; j++) {
				// you don't want to print *
				// rather, you want the value in the cell
				System.out.print (" " + board[i][j] + " ");
			}
	 		System.out.println();
		}
		
	}
	
	//3) A boolean method mCatsGame (will accept the char array and return a value of true if the board is completely filled with markers and neither player has won and a value of false otherwise)
	
	public boolean mCatsGame() {
		// needs work
		return false;
	}
		
	//4) A boolean method called mDidIWin (will accept the chararray and the player number and will return a valur of true if the player has won and a value of false otherwise.)
	public boolean mCatsGame(int playerNumber) {
		// needs work
		return false;
	}
	
	public void mPlay() {
		System.out.println ("This program allows you to play Tic-Tac-Toe");  
		System.out.println();
		mPopulateCard();
		//mPrintCard();
		// needs more work
	}
	
	// you'll be doing this for both players, so having such a method seem apropriate
	private void processPlayerInput(int playerNum) {
		System.out.println ("Player " + playerNum + ": Enter a row 1 - 3.");
		int row = input.nextInt();
		System.out.println ("Player " + playerNum + ": Enter a column 1 - 3.");
		int column = input.nextInt();
		// needs more work
	}

	public static void main( String args[]) { 
		TicTacToe pgm = new TicTacToe();
		pgm.mPlay();
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 safialan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-October 08

Re: TicTacToe class --> not an applet

Posted 18 November 2008 - 09:42 PM

Thank you very much for taking your time to answer my questions... I feel less lost, I will continue working on this assignment, at least I know where to start :)

Just a quick question --> I am trying to place the "X" according the location player 1 selects. So thought like this:


  for (int i =0; i<board.length; i++) 
{ for(int j=0;j<board[i].length;j++)
   {  if (board[ROWS][COLS] == "*")
   { System.out.print ("X"); }
	 if (board[ROWS][COLS] != "*")
  { System.out.print ("This location has already been selected. Please choose another");
		   }
			   }
			 }



But it does not compile :s any ideas???

This post has been edited by safialan: 18 November 2008 - 10:04 PM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: TicTacToe class --> not an applet

Posted 19 November 2008 - 05:40 AM

You're in a loop, but you're always looking at the same place, board[ROWS][COLS]. Worse, that place is board[3][3] which is out of bounds. Remember that "int a[3]" goes from a[0]..a[2] and a[3] is out of range. Also, if you want to check a single value in the array, you don't need a loop.

Consider this as a basic loop:
for (int row=0; row<ROWS; row++) {
	for(int col=0; col<COLS; col++) {
		// do something
		// almost always to board[row][col]
	}
}



So, to print the board:
for (int row=0; row<ROWS; row++) {
	for(int col=0; col<COLS; col++) {
		System.out.print(board[row][col]);
	}
	System.out.println();
}



To do the a check, it should be as simple as:
int row; // user selected value
int col; // user selected value
if (board[row][col] != "*") {
	System.out.print ("This location has already been selected. Please choose another");
}


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: TicTacToe class --> not an applet

Posted 19 November 2008 - 06:22 PM

if (board[row][col] != "*") {


should be
if (board[row][col] != '*') {


Was This Post Helpful? 0
  • +
  • -

#7 safialan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-October 08

Re: TicTacToe class --> not an applet

Posted 19 November 2008 - 06:33 PM

Thank you very much... Step by step I'm getting closer :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1