0

I've been trying to make this tick tack toe game using several static methods. I am relatively new to this still and can't quite figure out the whole idea of mouse clicks. I've read about the mouseclick and mouseevent, but it doesn't make complete sense and I get lots of errors when I attempt that. Originally I had the part where it takes the mouse info in its own method, but then I couldn't figure out how to return both the x and y values. So I add the method to fill the array underneath. Now I messed around with it and Managed to get them in their own methods but still have problems running the program. (they don't have to be in their own methods, I just figured it would simplify things) When I run this program, all it does is print an infinite amount of lines saying what row and column I clicked on and puts an O in the first row and first column regardless of if I click or not. Also, it seems it doesn't switch turns between players either. If anyone could help me, I would greatly appreciate it. Thanks!

import java.util.*;
public class Game {

  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 Point;


  public static void main (String args[]) {



    drawBoard();


    Fill();
  }

    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




  //get mouse click and turn into array spot
  public static void Mouse(){
    while(true){
      if (StdDraw.mousePressed()){
        a = StdDraw.mouseX();
        b = StdDraw.mouseY();
        System.out.println( a + " " + b);
      }



      //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 = (int)1;}
      if ( 6<=b && b< 9){
        y = 2;}
      System.out.println("You clicked in Row" + x + "and column" +y);
    }
  }

      public static void Fill(){
      //fill array
        Mouse();
      boolean validInput = false;
      do{    
      for (int i = 0 ; i <=9 ; i++){
    if (i % 2 == 0){
          currentPlayer = Cross;
        }
        else {
          currentPlayer = Oh;
        }}
          if (0 <= x && x<=2 && 0 <=y && y <= 2 && board[x][y] == 0){

            //fill array spot
        board[x][y] = currentPlayer;
        //check game status and print board
        GameStatus();
        PrintBoard();
        validInput = true; //input is good, exit the loop
        }
        else { 
          System.out.println("This move is not valid. Try again.");
          }
      }while (!validInput);
    }














    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
      }
      if (board[j][k] == 1){
        double l = ((j+1) * 3) - 1.5;
        double m = ((k+1) * 3) - 1.5;
        //print x
        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.text(l,m,"O");}
    }
  }
    }


  public static void GameStatus(){
        //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.text(0.5, 0.5, "X Won!");}
          //O win
          while (currentPlayer==-1){
            StdDraw.text(0.5, 0.5, "O Won!");}
          return;
        }

        //draw
        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.text(0.5, 0.5, "Cat's Game!");
          return;}

        //still playing
        else {
          System.out.println("Keep Playing.");
          //keep playing
        }

  }//Ends playerMove

}// end game

2 Answers2

0

First off, you have a "while(true)" statement. That's an infinite loop, and you'll need something to tell it to break out. That's why your seeing a spam of text.

Secondly, where is the StdDraw object you are using to get your "mousePressed" event? I recommend a "MOUSE_CLICKED" event if you are just getting a click, but even if you stay with the mousePressed, you need to make sure it isn't detecting it if the mouse is not pressed.

Rezkin
  • 441
  • 1
  • 6
  • 17
  • Thank you, I ended up making the whole Mouse method a part of the loop and putting a break at the end of the loop which fixed my problem. – Gabbie Grapes Nov 07 '16 at 21:16
  • My teacher wanted us to stay away from the mouse clicked events since we haven't really learned them yet and you can't call them directly. – Gabbie Grapes Nov 07 '16 at 21:17
0

It keeps printing the lines because you created an infinite loop in your Mouse() method, and it checks the conditions in the if-statements millions of times per second.

I would suggest creating a separate method called for example gameLoop() and call all other methods [carefully structured] from within the gameLoop().

In the stdlib package documentation we read:

[Keyboard and mouse inputs] You should use these methods in an animation loop that waits a short while before trying to poll the mouse for its current state.

So in your game you could try something like this:

gameLoop() {
    isPlaying = true;
    drawBoard();
    ...;
    while (isPlaying) {
        isPressed = false;
        checkMousePressed();
        if (isPressed) {
            checkMove();
            ...;
        }  
        Thread.sleep(100); // 100 ms, so you repeat the loop 10 times/sec.
    }
    printWinner();
}

Also, a good practice for method names is using verbs, not nouns. Thus, you say what the method does, in other words, you state its purpose, and consequently it is easier to follow the logic of the program. If you call it mouse() it is unclear what the method is supposed to do.

sattik
  • 71
  • 4