Tic Tac Toe循环/方法运行太多次

时间:2016-11-07 21:50:51

标签: java loops methods execution tic-tac-toe

更新:我发现问题出在Mouse()方法中。它会运行并取值,直到你放开鼠标。我需要一种方法来暂停程序或停止它直到有人释放鼠标。

我正在制作Tic Tac Toe游戏,我想我只能解决一个问题。我让它完美地工作,但后来我搞砸了尝试让消息出现在实际的游戏而不仅仅是java交互窗格(请原谅我,如果这些不是正确的术语) 由于某种原因,当我运行时,它将正确地进行第一次移动然后它就好像有人一直点击板上的相同位置并且多次给我一个无效的输入错误。我不知道该怎么做才能解决这个问题,但我猜这个错误是在Fill()方法中。任何提示或提示将不胜感激。 谢谢!

 /* 
 * Tic Tac Toe Game
 */
import java.util.*;
import java.awt.*;

public class Game {
    //Create Variables
    public static int x;
    public static int y;
    public static double a;
    public static double b;
    public static int empty = 0;
    public static int Cross = 1;
    public static int Oh = -1;
    public static double[][] board = new double[3][3];
    public static int currentPlayer;
    public static int p = 1;
    //public static boolean v = false;


    //Main method: draws the board and then begins game
    public static void main(String args[]) {
        initalize();
        drawBoard();
        // System.out.println("X's turn.");
        for(int w = 0; w < 9; w++) {
            turn();
            Fill();
        }
    }


    /* 
     * this method creates the graphic as a 9x9 board with the 'hashtag' for playing
     */
    public static void drawBoard() {
        StdDraw.setXscale(0, 9);
        StdDraw.setYscale(0, 9);
        StdDraw.setPenRadius(.01);
        StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.line(0, 3, 9, 3);
        StdDraw.line(0, 6, 9, 6);
        StdDraw.line(3, 0, 3, 9);
        StdDraw.line(6, 0, 6, 9);
    } //end draw board

    public static void initalize() {
        for(int j = 0; j <= 2; j++) {
            for(int k = 0; k <= 2; k++) {
                board[j][k] = 0;
            }
        }
    }

    /*
     * this method will take the coordinates of the mouse when clicked as a,b coordinates
     * the it will take the a,b coordinates and turn them into an x,y board position (0-2)
     */
    public static void Mouse() {
        while(true) {
            if(StdDraw.mousePressed()) {
                a = StdDraw.mouseX();
                b = StdDraw.mouseY();
                //System.out.println( a + " " + b);
                //board location
                //set column
                if(0 <= a && a < 3) {
                    x = 0;
                }
                if(3 <= a && a < 6) {
                    x = 1;
                }
                if(6 <= a && a < 9) {
                    x = 2;
                }
                //set row
                if(0 <= b && b < 3) {
                    y = 0;
                }
                if(3 <= b && b < 6) {
                    y = 1;
                }
                if(6 <= b && b < 9) {
                    y = 2;
                }
                //System.out.println("You clicked in Row" + x + "and column" +y);
                break;
            }
        }
    }//ends Mouse


    /*
     * Runs CurrentPlayer() to deicde who the current player is and then prints a message with whose turn it is
     */
    public static void turn() {
        CurrentPlayer();
        if(currentPlayer != Cross) {
            System.out.println("O's turn.");
        }
        if(currentPlayer != Oh) {
            System.out.println("X's turn.");
        }
    }

    /*
     * this method determines if it is player X or player O
     * it flip flops each time it is called and reurns the value of the 'currentPlayer' (-1 or 1)
     */
    public static int CurrentPlayer() {
        if(p % 2 == 0) {
            currentPlayer = Oh;
        } else {
            currentPlayer = Cross;
        }
        return currentPlayer;
    }//ends CurrentPlayer


    /*
     * this method is the actual 'playing'
     * it calls Mouse() to get coordinates/board location
     * determines if it is a valid move by making sure its within the board range and also checking to make sure that array spot of the board it empty(0) 
     * it then fills the array with the value of the currentplayer, prints the board, checks the game status for a win, adds a turn, and restarts if it is valid.
     * if its not vaild it will tell you and then restart to get a valid move
     */
    public static void Fill() {
        Mouse();
        //check if valid
        if(0 <= x && x <= 2 && 0 <= y && y <= 2 && board[x][y] == 0) {
            //switch player
            p++;
            //fill array spot
            board[x][y] = currentPlayer;
            //check game status and print board
            PrintBoard();
            GameStatus();
            return;//exit method
        }
        //move is not valid
        else {
            System.out.println("Invalid. Try again.");
            Fill();
        }
    }//ends Fill()

    /*
     *scrolls through board array to check for moves.
     * if array value=0, its empty and does nothing
     * if array value=1, its X and prints an X in that board position
     * if array value=-1, its O and prints an O in that board position
     */
    public static void PrintBoard() {
        for(int j = 0; j <= 2; j++) {
            for(int k = 0; k <= 2; k++) {
                if(board[j][k] == 0) {
                    //do nothing leave empty
                }
                Font mark = new Font("Arial", Font.BOLD, 60);

                if(board[j][k] == 1) {
                    double l = ((j + 1) * 3) - 1.5;
                    double m = ((k + 1) * 3) - 1.5;
                    //print x
                    StdDraw.setFont(mark);
                    StdDraw.setPenColor(StdDraw.RED);
                    StdDraw.text(l, m, "X");
                }
                if(board[j][k] == -1) {
                    double l = ((j + 1) * 3) - 1.5;
                    double m = ((k + 1) * 3) - 1.5;
                    //print O
                    StdDraw.setFont(mark);
                    StdDraw.setPenColor(StdDraw.BLUE);
                    StdDraw.text(l, m, "O");
                }
            }
        }
        return;
    }//Ends PrintBoard()

    /*
     * this method checks if there is a win, cats game, or if the game is still in play
     * if its a win it determines if it is an X win or O win and prints out a winners message coorespondingnto who won
     * if its a cats game, it prints out cats game
     * it the game is still in play it says keep playing and goes back to the Fill() method
     */
    public static void GameStatus() {
        Font win = new Font("Arial", Font.BOLD, 100);
        Font cat = new Font("Arial", Font.BOLD, 80);

        //check for win
        if(// First column
                board[0][0] == currentPlayer && board[0][1] == currentPlayer && board[0][2] == currentPlayer
                        //second column
                        || board[1][0] == currentPlayer && board[1][1] == currentPlayer && board[1][2] == currentPlayer
                        //third column
                        || board[2][0] == currentPlayer && board[2][1] == currentPlayer && board[2][2] == currentPlayer
                        //first row
                        || board[0][0] == currentPlayer && board[1][0] == currentPlayer && board[2][0] == currentPlayer
                        //second row
                        || board[0][1] == currentPlayer && board[1][1] == currentPlayer && board[2][1] == currentPlayer
                        //third row
                        || board[0][2] == currentPlayer && board[1][2] == currentPlayer && board[2][2] == currentPlayer
                        //diagonal 1
                        || board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer
                        // diagonal 2
                        || board[2][2] == currentPlayer && board[1][1] == currentPlayer && board[0][0] == currentPlayer) {
            //X win
            while(currentPlayer == 1) {
                StdDraw.setFont(win);
                StdDraw.setPenColor(StdDraw.GREEN);
                StdDraw.text(4.5, 4.5, "X Won!");
            }
            //O win
            while(currentPlayer == -1) {
                StdDraw.setFont(win);
                StdDraw.setPenColor(StdDraw.GREEN);
                StdDraw.text(4.5, 4.5, "O Won!");
            }
            return;
        }

        //cats game
        else if(board[0][0] != 0 && board[0][1] != 0 && board[0][2] != 0 && board[1][0] != 0 && board[1][1] != 0 && board[1][2] != 0 && board[2][0] != 0 && board[2][1] != 0 && board[2][2] != 0) {
            StdDraw.setFont(cat);
            StdDraw.setPenColor(StdDraw.YELLOW);
            StdDraw.text(4.5, 4.5, "Cat's Game!");
            return;
        }

        //still playing
        else {
            System.out.println("Keep Playing.");
            return;
        }
    }//Ends GameStatus
}// End Class Game

0 个答案:

没有答案