lundi 26 octobre 2015

Java: How do I tally the total wins of the players in a tic tac toe game?

In my program, I am trying to simulate 1000 games of randomized tic tac toe. I have set up if-else statements in an attempt to tally the wins for each player, and the ties. However the console is only returning one number for me. Below is my code for the program. Thank you in advance for any help.

public static void main(String[] args) {

    int count = 0;
    int xWins = 0;
    int oWins = 0;
    int ties = 0;


    Random r = new Random();
    int [][] list = new int [3][3];
    for (int row = 0;row < list.length; row++ ) {
        for (int col = 0; col<list[row].length; col++) {
            //list [row][col] = r.nextInt(3);
        }

    }

for (count = 0; count < 1000; count++) {
    int movecount = 0; 
    int row, col;
    int player = 1;
    do {
        //pick a row
        row = r.nextInt(3);
        //pick a col
        col = r.nextInt(3);
        //check if spot is empty
        if (list[row][col]>0) {continue;}
        //if empty, move current player there, add to count
        list[row][col] = player;
        if (CheckRowWin(player, list)) {
            if (player == 1) {
                xWins++;
            }
            else if (player == 2) {
                oWins++;
            }
            else {
                ties++;
            }

            break;
        } 

        movecount++;
        //switch player turn
        player = 3 - player;

    } while (movecount < 9);
    movecount = 0;

    System.out.printf("x wins: %d, o wins: %d, ties: %d\n", xWins, oWins, ties);
    }

Aucun commentaire:

Enregistrer un commentaire