Welcome to Dream.In.Code
Become a Java Expert!

Join 149,489 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,288 people online right now. Registration is fast and FREE... Join Now!




Tic Tac Toe

 
Reply to this topicStart new topic

Tic Tac Toe

tukeywilliams
22 Mar, 2007 - 12:36 AM
Post #1

New D.I.C Head
*

Joined: 22 Mar, 2007
Posts: 3


My Contributions
I am trying to run this , but it is not working:

CODE


/*
* This is a program for Tic-Tac-Toe. The board
* is represented by a 2-D array.
*
*/

import java.util.*;
public class TicTacToe
{
  static char[][] board;
  public static void main(String[] args) throws java.io.IOException
  {
    initialize();
    char_player = 'X';
    while(game_not_over())
    {
      player_moves(player);
      player = other_player(player);
      print_board();
    }
    print_winner();
  }
  
  static void initialize()
  {
    board = new char[3][3];
    for(int row = 0; row < 3; row++)
    {
       for(int column = 0; column < 3; column++)
       {
         board[row][column] = '_';
       }
    }
  }
  
  static boolean game_not_over()
  {
    boolean a_winner;
    
    a_winner = win('X');
    if(a_winner) return false;
    a_winner = win('0');
    if(a_winner) return false;
    
    int count_moves = 0;
    for(int row = 0; row < 3; row++)
    {
      int three_in_a_row = 0;
      for(int column = 0; column < 3; column++)
      {
        if(board[row][column] != '_') count_moves++;
      }
    }
    if(count_moves == 9) return false;
    return true;
  }
  
  static void player_moves(char player) throws java.io.IOException
  {
    Scanner s = new Scanner(System.in);
    
    int move = 0;
    boolean move_ok = false;
    while(! move_ok)
    {
      System.out.println("Enter position for next "+player+" move");
      move = s.nextInt();
      move_ok = ok_move(move);
    }
    mark(move.player);
  }
  
  static char other_player(char player)
  {
    if(player == 'X') return 'O';
    else return 'X';
  }
  static void print_board()
  {
    for(int row = 0; row < 3; row++)
    {
      for(int column = 0; column < 3; column++)
      {
        System.out.print(board[row][column]+" ");
      }
      System.out.println();
    }
  }
  
  static void print_winner()
  {
    if(win('X')) System.out.println("X is the winner");
    else if(win('O')) System.out.println("O  is the winner");
    else System.out.println("I guess that cat won.");
  }
  
  static boolean win(char xo)
  {
    if(board[0][0]== xo && board[0][1] == xo && board[0][2] == xo)
      return true;
    if(board[1][0] == xo && board[1][1] == xo && board[1][2] == xo)
      return true;
    if(board[2][0]== xo && board[2][1] == xo && board[2][2] == xo)
      return true;
    
    if(board[0][0] == xo && board[1][0] == xo && board[2][0] == xo)
      return true;
    if(board[0][1] == xo && board[1][1] == xo && board[2][1] == xo)
      return true;
    if(board[0][2] == xo && board[1][2] == xo && board[2][1] == xo)
      return true;
    
    if(board[0][0] == xo && board[1][1] == xo && board[2][2] == xo)
      return true;
    if(board[0][2] == xo && board[1][1] == xo && board[2][0] == xo)
      return true;
     return false;
     }
    
  
  static void mark(int position, char x_or_o)
  {
    int row = (position-1)/3;
    int column = (position-1)%3;
    board[row][column] = x_or_o;
  }
  }
        
    
    
      
  
      
  
        



User is offlineProfile CardPM
+Quote Post

horace
RE: Tic Tac Toe
22 Mar, 2007 - 01:38 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
for a start
CODE

  public static void main(String[] args) throws java.io.IOException
  {
    initialize();
    char_player = 'X';

the last line should be
CODE

   char player = 'X';

now see if you can find any more errors
User is offlineProfile CardPM
+Quote Post

tukeywilliams
RE: Tic Tac Toe
22 Mar, 2007 - 04:38 PM
Post #3

New D.I.C Head
*

Joined: 22 Mar, 2007
Posts: 3


My Contributions
QUOTE(horace @ 22 Mar, 2007 - 02:38 AM) *

for a start
CODE

  public static void main(String[] args) throws java.io.IOException
  {
    initialize();
    char_player = 'X';

the last line should be
CODE

   char player = 'X';

now see if you can find any more errors


CODE

public class TicTacToe
{
  static char[][] board;
  public static void main(String[] args)
  {
    initialize();
    char player = 'X';
    while(inGame())
    {
      player_moves(player);
      player = switch_player(player);
      print_board();
    }
    print_winner();
  }
  
  void initialize()
  {
     board = new char[3][3];
     for(int i = 0; i< 3; i++)
     {
        for(int j = 0; j < 3; j++)
        {
          board[i][j] = '_';
        }
     }
  }
  
  boolean inGame()
  {
    boolean winner;
    winner = win('X');
    if(winner)
      return false;
    winner = win('O');
    if(winner)
      return false;
  
    int count = 0;
    for(int i = 0; i < 3; i++)
    {
      int three_in_a_row = 0;
      for(int j = 0; j < 3; j++)
      {
        if(board[i][j] != '_')
        count++;
      }
    }
    if(count == 9)
      return false;
      return true;
  }
  
  void player_moves(char player)
  {
     Scanner s = new Scanner(System.in);
     int move = 0;
     boolean move_legal = false;
     while(! move_legal)
     {
       System.out.println("Enter number for next " +player+ "move.");
       move = s.nextInt();
       move_legal = legal_move(move);
     }
     draw(move,player);
  }
  
  char switch_player(char player)
  {
     if(player == 'X')
       return 'O';
     else
       return 'X';
  }
  
   void print_board()
  {
    for(int i = 0; i < 3; i++)
    {
      for(int j = 0; j < 3; j++)
      {
        System.out.println(board[i][j] + " ");
      }
      System.out.println();
    }
  }
  
  void print_winner()
  {
    if(win('X'))
    System.out.println("X is the winner");
    
    else if(win('O'))
    System.out.println("O is the winner");
    
    else
    System.out.println("No winner");
  }
  
  boolean win(char c)
  {
    if(board[0][0] == c && board[0][1] == c && board[0][2] == c)
      return true;
    
    if(board[1][0] == c && board [1][1] == c && board[1][2] == c)
      return true;
    
    if(board[2][0] == c && board[2][1] == c && board[2][2] == c)
      return true;
    
    if(board[0][0] == c && board[1][0] == c && board[2][0] == c)
      return true;
    
    if(board[0][1] == c && board[1][1] == c && board[2][1] == c)
      return true;
    
    if(board[0][2] == c && board[1][2] == c && board[2][2] == c)
      return true;
    
    if(board[0][0] == c && board[1][1] == c && board[2][2] == c)
      return true;
    
    if(board[0][2] == c && board[1][1] == c && board[2][0] == c)
      return true;
    return false;
    
  boolean legal_move(int position)
  {
    if(position < 1 || position > 9)
    {
       System.out.println("Error: invalid position" + position);
       return false;
    }
    else
    {
      int i = (position - 1)/3;
      int j = (position -1)%3;
      if(board[i][j] != '_')
      {
        System.out.println("You can't move here. The square is occupied.");
        return false;
      }
      return true;
    }
  }
  
  void draw(int position, char c)
  {
    int i = (position - 1)/3;
    int j = (position - 1)%3;
    board[i][j] = c;
  }
  }
}



I am getting a "; expected" error on line 127. I cant sem to find the error (i.e. there are semicolons after each statement). Also, in terms of efficiency (less running time), would a switch statement be more efficient than a series of if statements?

This post has been edited by tukeywilliams: 22 Mar, 2007 - 04:39 PM
User is offlineProfile CardPM
+Quote Post

horace
RE: Tic Tac Toe
22 Mar, 2007 - 10:45 PM
Post #4

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
there is probably a ; missing off the end of the previous line


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 05:19PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month