Results 1 to 2 of 2
  1. #1
    jokerboy123 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default Help with TicTacToe

    I dont know why but when this trie to function addEntry();
    it just replaces my scoreboard but doesnt update it, anyone able to tell me why this is? i have a feeling its a stupid reason.

    heres my code:


    /**
    * Write a description of class TicTacToe here.
    *
    * @author (your name)
    * @version (a version number or a date)
    */
    import java.io.*;
    import java.util.*;

    public class TicTacToe
    {
    private String name1;
    private String name2;
    private String winner;
    private String loser;
    private int player1;
    private int player2;
    private int turn;
    private String[][] board;
    public static String[] scoreBoardNames;
    public static int[][] scoreBoardNums;
    private int boardCount;
    public static boolean gameOver;
    public static boolean tie;
    public static boolean playing;

    public TicTacToe()
    {
    name1 = "";
    name2 = "";
    winner = "";
    loser = "";
    turn = 2;
    boardCount = 0;
    player1 = 0;
    player2 = 1;

    TicTacToe.tie = false;
    TicTacToe.playing = true;
    board = new String[3][];
    for(int r = 0; r < 3; r++)
    {
    board[r] = new String[3];
    for(int c = 0; c < 3; c++)
    {
    board[r][c] = " ";
    }
    }
    scoreBoardNames = new String[10];
    for(int x = 0; x < 10; x++)
    {
    scoreBoardNames[x] = " ";
    }
    scoreBoardNums = new int[10][];
    for(int row = 0; row < 10; row++)
    {
    scoreBoardNums[row] = new int[2];
    for(int col = 0; col < 2; col++)
    {
    scoreBoardNums[row][col] = 0;
    }
    }
    }

    public void print()
    {
    if(TicTacToe.gameOver == true)
    {
    System.out.println("Players-");
    System.out.println("Player 1 (X): " + name1);
    System.out.println("Player 2 (O): " + name2 + "\n");

    System.out.println(" " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " ");
    System.out.println("--- --- ---");
    System.out.println(" " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " ");
    System.out.println("--- --- ---");
    System.out.println(" " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " \n");
    System.out.println("Player " + winner + " wins!\n");
    }
    else if(name1.equals("") && name2.equals(""))
    {
    System.out.println("Players-");
    System.out.println("Player 1 (X): " + name1);
    System.out.println("Player 2 (O): " + name2 + "\n");

    System.out.println(" " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " ");
    System.out.println("--- --- ---");
    System.out.println(" " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " ");
    System.out.println("--- --- ---");
    System.out.println(" " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " \n");
    System.out.println("There are no players!\n");
    }
    else if(turn == player1)
    {
    System.out.println("Players-");
    System.out.println("Player 1 (X): " + name1);
    System.out.println("Player 2 (O): " + name2 + "\n");

    System.out.println(" " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " ");
    System.out.println("--- --- ---");
    System.out.println(" " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " ");
    System.out.println("--- --- ---");
    System.out.println(" " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " \n");
    System.out.println("It is now " + name1 + "'s turn.\n");
    }
    else if(turn == player2)
    {
    System.out.println("Players-");
    System.out.println("Player 1 (X): " + name1);
    System.out.println("Player 2 (O): " + name2 + "\n");

    System.out.println(" " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " ");
    System.out.println("--- --- ---");
    System.out.println(" " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " ");
    System.out.println("--- --- ---");
    System.out.println(" " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " \n");
    System.out.println("It is now " + name2 + "'s turn.\n");
    }
    System.out.println("-------------------------------------");
    }

    public int startGame(String a, String b)
    {
    name1 = a;
    name2 = b;
    TicTacToe.gameOver = false;

    Random rand = new Random();
    turn = rand.nextInt(2);

    return turn;
    }

    public boolean addMove(String actPlayer, int row, int col)
    {
    try
    {
    if(board[row][col].equals(" "))
    {
    while(turn == player1)
    {
    if(actPlayer.equals(name1))
    {
    turn = player2;
    boardCount++;
    board[row][col] = "X";
    winner = name1;
    loser = name2;
    return true;
    }


    else
    {
    System.out.println("It is not your turn!");
    return false;
    }
    }
    while(turn == player2)
    {
    if(actPlayer.equals(name2))
    {
    turn = player1;
    boardCount++;
    board[row][col] = "O";
    winner = name2;
    loser = name1;
    return true;
    }
    else
    {
    System.out.println("It is not your turn!");
    return false;
    }
    }
    }
    else
    {
    System.out.println("That space is full!");
    }
    }
    catch (Exception e)
    {//Catch exception if any
    System.out.println("That is outside the board!");
    }


    return false;
    }

    public void checkWin()
    {
    if(board[0][0].equals(board[0][1]) && board[0][1].equals(board[0][2]) && !board[0][0].equals(" "))
    {
    TicTacToe.gameOver = true;
    TicTacToe.playing = false;
    }
    else if(board[1][0].equals(board[1][1]) && board[1][1].equals(board[1][2]) && !board[1][0].equals(" "))
    {
    TicTacToe.gameOver = true;
    TicTacToe.playing = false;
    }
    else if(board[2][0].equals(board[2][1]) && board[2][1].equals(board[2][2]) && !board[2][0].equals(" "))
    {
    TicTacToe.gameOver = true;
    TicTacToe.playing = false;
    }
    else if(board[0][0].equals(board[1][0]) && board[1][0].equals(board[2][0]) && !board[0][0].equals(" "))
    {
    TicTacToe.gameOver = true;
    TicTacToe.playing = false;
    }
    else if(board[0][1].equals(board[1][1]) && board[1][1].equals(board[2][1]) && !board[0][1].equals(" "))
    {
    TicTacToe.gameOver = true;
    TicTacToe.playing = false;
    }
    else if(board[0][2].equals(board[1][2]) && board[1][2].equals(board[2][2]) && !board[0][2].equals(" "))
    {
    TicTacToe.gameOver = true;
    TicTacToe.playing = false;
    }
    else if(board[0][0].equals(board[1][1]) && board[1][1].equals(board[2][2]) && !board[0][0].equals(" "))
    {
    TicTacToe.gameOver = true;
    TicTacToe.playing = false;
    }
    else if(board[2][0].equals(board[1][1]) && board[1][1].equals(board[0][2]) && !board[2][0].equals(" "))
    {
    TicTacToe.gameOver = true;
    TicTacToe.playing = false;
    }
    else if(boardCount == 9)
    {
    TicTacToe.tie = true;
    TicTacToe.playing = false;
    }
    }

    public void addEntry()
    {

    if(TicTacToe.playing == false)
    {
    int count = 0;
    for(int row = 0; row < 10; row++)
    {
    if(TicTacToe.scoreBoardNames[row].equals(winner))
    {
    TicTacToe.scoreBoardNums[row][0] = TicTacToe.scoreBoardNums[row][0] + 1;

    TicTacToe.scoreBoardNums[row][1] = TicTacToe.scoreBoardNums[row][1] + 1;

    count++;
    }
    else if(TicTacToe.scoreBoardNames[row].equals(loser))
    {
    TicTacToe.scoreBoardNums[row][0] = TicTacToe.scoreBoardNums[row][0] + 0;

    TicTacToe.scoreBoardNums[row][1] = TicTacToe.scoreBoardNums[row][1] + 1;

    count++;
    }
    else if(TicTacToe.scoreBoardNames[row].equals(" ") && count < 1)
    {
    TicTacToe.scoreBoardNames[row] = winner;
    TicTacToe.scoreBoardNums[row][0] = TicTacToe.scoreBoardNums[row][0] + 1;
    TicTacToe.scoreBoardNums[row][1] = TicTacToe.scoreBoardNums[row][1] + 1;

    count++;
    }
    else if(TicTacToe.scoreBoardNames[row].equals(" ") && count < 2)
    {
    TicTacToe.scoreBoardNames[row] = loser;
    TicTacToe.scoreBoardNums[row][0] = TicTacToe.scoreBoardNums[row][0] + 0;
    TicTacToe.scoreBoardNums[row][1] = TicTacToe.scoreBoardNums[row][1] + 1;
    count++;
    }
    }

    }



    TicTacToe.gameOver = true;
    }

    public void scoreBoard()
    {
    System.out.println("\tName\t\tWins\tTotal Games\n");
    for(int row = 0; row < 10; row++)
    {
    int counter = 0;
    System.out.print(row+1 + ".\t");
    System.out.print(TicTacToe.scoreBoardNames[row] + "\t\t");
    for(int col = 0; col < 2; col++)
    {
    if(counter < 1)
    {
    System.out.print(TicTacToe.scoreBoardNums[row][col] + "\t\t");
    counter++;
    }
    else
    {
    System.out.println(TicTacToe.scoreBoardNums[row][col]);
    }
    }
    }
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    Please code tags to make what you posted readable. Put [CODE] at the start of your code and [/CODE] at the end.

    it just replaces my scoreboard but doesnt update it

    It's not really clear what this means. Perhaps it would be clearer if you posted code that could be run (and said what the desired behaviour should be for some given input).

    One thing I did note is that you have made various variables "static" in the TicTacToe class; variables that represent things that are not in fact static (as in being qualities of the class rather than its instances). Probably nothing in your app (save the main() method in some class) should be static. It is also quite possible that the scoreboard should be represented by an instance of a separate class.
    Last edited by pbrockway2; 01-22-2011 at 02:33 AM.

Similar Threads

  1. TicTacToe game
    By pinotje in forum New To Java
    Replies: 9
    Last Post: 12-23-2010, 06:49 PM
  2. Tictactoe game
    By Fowler in forum New To Java
    Replies: 11
    Last Post: 10-31-2010, 10:55 PM
  3. Help with tictactoe
    By ximenaacucr in forum New To Java
    Replies: 3
    Last Post: 09-16-2009, 09:30 PM
  4. TicTacToe
    By Thomas Covington in forum New To Java
    Replies: 1
    Last Post: 04-10-2008, 05:51 AM
  5. TicTacToe
    By Joe3161 in forum New To Java
    Replies: 4
    Last Post: 11-25-2007, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •