Jump to content


Check out our Community Blogs

ultimate99

Member Since 27 Apr 2012
Offline Last Active May 09 2012 03:55 AM
-----

Posts I've Made

In Topic: Is This Looping Forever?

28 April 2012 - 10:49 AM

The output currently is:

Welcome to TicTacToe
0 1 2
0	|   |  
---+---+---
1	|   |  
---+---+---
2	|   |  

Choose a symbol: X or O

I want it to be changed to:


Welcome to TicTacToe

Game board:
[1][2][3]
[4][5][6]
[7][8][9]

In Topic: Is This Looping Forever?

28 April 2012 - 09:23 AM

I have not looked at the code. Take a look and see what changes it would require.


Yeah, that's what I've trying to figure out; what changes would it require? :laugh: :confused:

In Topic: Is This Looping Forever?

28 April 2012 - 08:46 AM

Are you asking how to map numbers 1-9 to row and column? Something like the following
subtract 1 and
% by number of columns to get column
/ by number of rows to get row


Yes, exactly! An output that would like this:
Game board:
[1][2][3]
[4][5][6]
[7][8][9]

and when player or computer plays, it appears like this:
Game board:
[X][2][3]
[X][O][6]
[O][8][X]

Does this involve a lot of modifications to the current code?

In Topic: Is This Looping Forever?

28 April 2012 - 08:34 AM

I've changed my code totally.

The board appears to display properly, but how can I have each cell numbered and ask the player to to choose a numbered cell to play? Instead of having the player choose "row number" and "column number"?

I've thought of this code, but how can I implement that?
char[][] board = new char[3][3];
  board[0][0] = '1';
		 board[0][1] = '2';
		 board[0][2] = '3';
		 board[1][0] = '4';
		 board[1][1] = '5';
		 board[1][2] = '6';
		 board[2][0] = '7';
		 board[2][1] = '8';
		 board[2][2] = '9';

Original code:
import java.util.Scanner;
public class TicTacToe{

static Scanner object = new Scanner(System.in);

public static void main(String[] args){
 
  //Game board size
  final int SIZE = 3;
  char[][] board = new char[SIZE][SIZE];
 
  //Initialize board
  resetBoard(board);
 
  //Display welcome message and display board
  System.out.print("Welcome to TicTacToe");
  showBoard(board);
 
  //Let the player choose which symbol he wants
  System.out.print("\nChoose a symbol: X or O");
  char playerSymbol = object.next().toLowerCase().charAt(0);
  char computerSymbol = (playerSymbol == 'x') ? 'o' : 'x';
 
  //Ask player to go first or not.
  System.out.println();
  System.out.print("Do you want to go first? (y/n)");
  char answer = object.next().toLowerCase().charAt(0);
 
  //Turns: player 0, computer 1
  int turn;
  //Empty positions
  int remainPosition = SIZE * SIZE;
 
  //First move
  if(answer == 'y'){
   turn = 0;
   //player chooses the position on the board
   playerTurn(board, playerSymbol);
  }
  else{
   turn = 1;
   //computer chooses its position on the board
   computerTurn(board, computerSymbol);
  }
  //Show board
  showBoard(board);
  //decrement remaining positions on board
  remainPosition--;
 
  boolean done = false;
  int winner = -1;
 
  while(!done && remainPosition > 0){
   done = isWon(board, turn, playerSymbol, computerSymbol);
   if(done)
    winner = turn;
   else{
    turn = (turn + 1) % 2;
    if(turn == 0)
	 playerTurn(board, playerSymbol);
    else
	 computerTurn(board, computerSymbol);
   
    showBoard(board);
    remainPosition--;
   
   }
  }
 
  //Check for winner
  //If winner found, declare the winner
  if(winner == 0)
   System.out.println("You won!");
  else if(winner == 1)
   System.out.println("You lost!");
  else
   System.out.println("Tie!");
 
}
private static void resetBoard(char[][] board) {
  for(int i = 0; i < board.length; i++)
   for(int j = 0; j < board[0].length; j++)
    board[i][j] = ' ';
 
}
private static void showBoard(char[][] board) {
  int numRow = board.length;
  int numColumn = board[0].length;
 
  System.out.println();
 
  //Column header
  System.out.print(" ");
  for(int i = 0; i < numColumn; i++);
   System.out.print(i + " ");
  System.out.print('\n');
 
  //Blank after header
  System.out.println();
 
  //Table
  for(int i = 0; i < numRow; i++){
   System.out.print(i + " ");
   for(int j = 0; j < numColumn; j++){
    if(j !=0)
	 System.out.print("|");
    System.out.print(" " + board[i][j] + " ");
   }
  
   System.out.println();
  
   if(i != (numRow - 1)){
    //Line separator
    System.out.print(" ");
    for(int j = 0; j < numColumn; j++){
	 if(j != 0)
	  System.out.print("+");
	 System.out.print("---");
    }
    System.out.println();
   }
  }
  System.out.println();
}

private static boolean isWon(char[][] board, int turn, char playerSymbol,
   char computerSymbol) {
  char symb;
  if(turn == 0)
   symb = playerSymbol;
  else
   symb = computerSymbol;
 
  int i, j = 0;
  boolean win = false;
 
  //Check win from row
  for(i = 0; i < board.length && !win; i++){
   for(j = 0; j < board[0].length; j++){
    if(board[i][j] != symb)
	 break;
   }
   if (j == board[0].length)
    win = true;
  }
 
  // Check win from column
	 for (j = 0; j < board[0].length && !win; j++) {
	   for (i = 0; i < board.length; i++) {
		 if (board[i][j] != symb)
		   break;
	   }
	   if (i == board.length)
		 win = true;
	 }
 
  //Check win diagonally (1)
  if(!win){
   for(i = 0; i < board.length; i++){
    if(board[i][i] != symb)
	 break;
   }
   if(i == board.length)
    win = true;
  }
 
  //Check win diagonally (2)
  if (!win){
   for(i = 0; i < board.length; i++){
    if(board[i][board.length - 1 - i] != symb)
	 break;
   }
   if(i == board.length)
    win = true;
  }
  //Retrun win
  return win;
}
private static void computerTurn(char[][] board, char computerSymbol) {
  for(int i = 0; i < board.length; i++){
   for(int j = 0; j < board[0].length; j++){
    if(board[i][j] == ' '){
	 board[i][j] = computerSymbol;
	 return;
    }
   }
  }
 
}
private static void playerTurn(char[][] board, char playerSymbol) {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter row #, hit enter, column #, hit enter again: ");
  int rowNumber = sc.nextInt();
  int columnNumber = sc.nextInt();
 
  while(board[rowNumber][columnNumber] != ' '){
   System.out.print("Position taken. \nEnter row and column numbers: ");
   rowNumber = sc.nextInt();
   columnNumber = sc.nextInt();
  }
  board[rowNumber][columnNumber] = playerSymbol;
 
}



}

In Topic: Java:Tutorial - Tic-Tac-Toe

27 April 2012 - 07:27 AM

Leave out the GUI. Don't handle Button/Mouse events, just handle keyboard input using an object that'll read from System.in.


Yes, I tried that. Left out buttons, JFrame, active listener..etc, but it messes up all the code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download