File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Stymied on tic tac toe project Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stymied on tic tac toe project" Watch "Stymied on tic tac toe project" New topic
Author

Stymied on tic tac toe project

John Snoddy
Greenhorn

Joined: Dec 02, 2008
Posts: 1
A simple tic tac toe project due (last week actually) and am at a loss in finally finishing it and the current errors. Any help would be greatly appreciated!

//December 01, 2008
//CIS 130 Lab9_TicTacToe1

import java.io.*;
import java.util.*;

public class Lab9_TicTacToe1
{
public static void main (String args [])
{
int moves, curr_player, cell, winner;

Scanner kb=new Scanner(System.in);

//string variable for names of players (player1 and player2)
moves=0;
cell=0;

int board[]=new int[9];

while((moves<9) && (winner == 0))
{
//input cell - if the cell is unoccupied (check value for 0)
{
moves++;

//set the br[cell] = curr_player;
//use switch statement to determine if that move makes the user a winner

switch(cell)
{
case 0:
if((br[0] == curr_player) && (br[1] == curr_player) && (br[2]==curr_player))
System.out.println("The winner is " + curr_player);
break;

case 1:
if((br[3] == curr_player) && (br[4] == curr_player) && (br[5]==curr_player))
System.out.println("The winner is " + curr_player);
break;

case 2:
if((br[6] == curr_player) && (br[7] == curr_player) && (br[8]==curr_player))
System.out.println("The winner is " + curr_player);
break;

case 3:
if((br[0] == curr_player) && (br[3] == curr_player) && (br[6]==curr_player))
System.out.println("The winner is " + curr_player);
break;

case 4:
if((br[1] == curr_player) && (br[4] == curr_player) && (br[7]==curr_player))
System.out.println("The winner is " + curr_player);
break;

case 5:
if((br[2] == curr_player) && (br[5] == curr_player) && (br[8]==curr_player))
System.out.println("The winner is " + curr_player);
break;

case 6:
if((br[0] == curr_player) && (br[4] == curr_player) && (br[8]==curr_player))
System.out.println("The winner is " + curr_player);
break;

case 7:
if((br[2] == curr_player) && (br[4] == curr_player) && (br[6]==curr_player))
System.out.println("The winner is " + curr_player);
break;

//similarly check other possible winning combination including 0 cell
}

//After getting out of switch display the board

if (winner == 0)
{
if(player == 1)
System.out.println(player = 2);

else
System.out.println(player = 1);
}
moves++;
}
}//end of for checking valid move

else
System.out.println("Invalid move.");
}//end while

if(winner != 0)
{
if(curr_player==1)
System.out.println(player2 + " is the winner.");
}
else
System.out.println("Game ended without a winner.");
}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18504
    
  40

A simple tic tac toe project due (last week actually) and am at a loss in finally finishing it and the current errors.


Well, what are these errors that you are encountering? Are they compile errors? runtime errors?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stymied on tic tac toe project
 
Similar Threads
Alpha beta pruning with negamax
Knight's tour
Connect-four
Having NullPointerException Error in Connect4
synchronization.. and threads !!!