Skip to main content

Can't seem to get my method (in Java) to compile correctly

I am trying to figure out exactly what is wrong with my winorTie method in this little tictacttoe game I am trying to create. Would anyone be able to help? Thanks

package tictactoegame;

/**
 *
 * @author Douglas Boulden
 */
public class tictactoegame {

    static int [][] gameboard;
    static final int EMPTY = 0;
    static final int NOUGHT = -1;
    static final int CROSS = 1;

    static void set (int val, int row) throws IllegalArgumentException {
        int col = 0;
        if (gameboard[row][col] == EMPTY)
                gameboard[row][col] = val;
        else throw new
            IllegalArgumentException("Player already there!");
    }

    static void displayBoard () {
        for (int[] gameboard1 : gameboard) {
            System.out.print("|");
            for (int c = 0; c < gameboard1.length; c++) {
                switch (gameboard1[c]) {
                    case NOUGHT:
                        System.out.print("0");
                        break;
                    case CROSS:
                        System.out.print("X");
                        break;
                    default:           //Empty
                        System.out.print(" ");
                }
                System.out.print("|");
            }
            System.out.println("\n------\n");
        }
    }

    static void createBoard(int rows, int cols) {
        gameboard = new int [rows] [cols];
    }

    static int winOrTie() {
       if (gameboard [0][0] == NOUGHT && gameboard [0][-1])
           return NOUGHT;
    } else if (gameboard [0][0] == && CROSS) [0][1]  {
           return CROSS;
    } else if (gameboard [0][0]== && " "()) [0][0] {    
           return 0;
    } else {
           return false;                
    }


    /**
     * @param args the command line arguments
     */    /**
     * @param args the command line arguments
     */

    public static void main(String[] args)  {
        createBoard(3,3);
        int turn = 0;
        int playerVal;
        int outcome;
        java.util.Scanner scan = new
            java.util.Scanner(System.in);
        do {
            displayBoard();
            playerVal = (turn % 2 == 0)? NOUGHT : CROSS;
            if (playerVal == NOUGHT) {
                System.out.println ("\n-0's turn-");
            } else {
                System.out.println("\n-X's turn-");
                }
            System.out.print("Enter row and Column:");
            try {
                set(playerVal, scan.nextInt());
            } catch (IllegalArgumentException ex)
            {System.err.println(ex);}
            turn ++;
            outcome = winOrTie();
        } while ( outcome == -2 );
        displayBoard();
        switch (outcome) {
            case NOUGHT:
                System.out.println("0 wins!");
                break;
            case CROSS:
                System.out.println("X wins!");
                break;
            case 0:
                System.out.println("Tie.");
                break;
            }
     }

}

Solved

Some of this was mentioned in the comments, but these conditions fundamentally don't make sense:

if (gameboard [0][0] == NOUGHT && gameboard [0][-1])
       return NOUGHT;
} else if (gameboard [0][0] == && CROSS) [0][1]  {
       return CROSS;
} else if (gameboard [0][0]== && " "()) [0][0] {    
       return 0;

For example, what do you think that if (gameboard [0][0] == && CROSS) [0][1] is supposed to do? What exactly is " "() supposed to be? And what do you think that == && does? It's difficult to know exactly what you were actually trying to achieve here.

Also, consider gameboard [0][-1]. There are two problems here. First, you do realize that -1 isn't actually a valid array index in Java, right? (That's allowable in Python, but not Java). Also, gameboard [0][-1] is an integer, not a bool, so && gameboard [0][-1] doesn't make sense. If you have something like A && B, both A and B must evaluate to some kind of boolean value (i.e. true or false).

Also, I'd encourage you not to do indentation like you have here. I'd recommend putting each "else if" on its own line.


Comments

Popular posts from this blog

C# How to spawn Enemies and move them? [closed]

I am trying to spawn enemies and move them from left to right with a timer how can i do this? Example: int count; timer (every 1 sec) { Enemy somename+count = new Enemy(); count++; } timer (every 0.001 sec) { somename+count.x++; } Solved Your fundamental problem seems to be lacking a way to store references to all of your enemies. Generics and collections provide a solution to this type of problem. I'll use a List , which is the most basic such collection. There are many others, however, that each have their own useful properties. Consider : private class Enemy { public int position; } private List enemyList = new List (); private void timer1_Tick(object sender, EventArgs e) { enemyList.Add(new Enemy()); } private void timer2_Tick(object sender, EventArgs e) { foreach(Enemy enemy in enemyList) { enemy.position++; } } Also consider that updating a large list of visual objects every 0.001 second is useless and wast...

Android : MPAndroidChart how to remove values on right side in chart?

I have the following chart which is displaying some values. I would like to hide/ remove values on the right side, because on the left side are enough. See image below: And code is following: public class MainActivity extends AppCompatActivity implements OnChartValueSelectedListener{ private LineChart mDataLineChart; private RelativeLayout mRelativeLayout; private LineChart mChart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_linechart); // Set chart setChart(); // add data setData2(3, 155); // ANIMATE CHART animateChart(); } private void setChart() { mChart = (LineChart) findViewById(R.id.chart1); mChart.setOnChartValueSelectedListener(...