Jump to content
3 minutes ago, Teemu249 said:

I figured the random thing and now I just get random crashes that probably are related to not checking inputs and so on. Here is my updated code if you want to look at the changes:

That was on my mind earlier but then it whisked away :P

 

Also a tidbit about your random number generation: find a way to make it so the random number generator will always give you the values you need within the bounds so you don't have to use the while-loops to re-roll the number.

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/page/2/#findComment-12065946
Share on other sites

Link to post
Share on other sites

2 minutes ago, M.Yurizaki said:

That was on my mind earlier but then it whisked away :P

 

Also a tidbit about your random number generation: find a way to make it so the random number generator will always give you the values you need within the bounds so you don't have to use the while-loops to re-roll the number.

Yeah I know it is not perfect but I will focus on getting this work for now. I will try to make it cleaner if I have time but deadline is in 7 hours and 30 mins haha(meaning I am working overnight).

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/page/2/#findComment-12065957
Share on other sites

Link to post
Share on other sites

2 minutes ago, mshaugh said:

Your current implementation is the likely cause of these crashes. How many function calls do you think there are before your ai has a valid move?

Okey then, hmm will take a look at that now haha. Thank you! Do you have any direct suggestions or?

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/page/2/#findComment-12066006
Share on other sites

Link to post
Share on other sites

4 minutes ago, mshaugh said:

Replace your (int) Math.random() * 100 with ThreadLocalRandom.current().nextInt(), which you want to be bounded by the size of the game board.

Never used this before I can't make sense of the syntax..could you help me with placing values into the line?

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/page/2/#findComment-12066023
Share on other sites

Link to post
Share on other sites

Just now, Teemu249 said:

Never used this before I can't make sense of the syntax..could you help me with placing values into the line?

import java.util.concurrent.ThreadLocalRandom;

...

int aiMoveVertical = ThreadLocalRandom.current.nextInt(10); // 0 <= aiMoveVertical < 9

 

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/page/2/#findComment-12066028
Share on other sites

Link to post
Share on other sites

Just now, mshaugh said:

import java.util.concurrent.ThreadLocalRandom;

...

int aiMoveVertical = ThreadLocalRandom.current.nextInt(10); // 0 <= aiMoveVertical < 9

 

This is dynamic tictactoe meaning board size is user configurable so I assume i put "size" in place of 10?

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/page/2/#findComment-12066034
Share on other sites

Link to post
Share on other sites

3 minutes ago, mshaugh said:

This will give you 0 <= aiMove < size. That's what you want, so yes.

Filled the whole board without crashing!

int aiMoveVertical = ThreadLocalRandom.current().nextInt(1, size);
        
        int aiMoveHorizontal = ThreadLocalRandom.current().nextInt(1, size);


Now it's my turn

     0   1   2   3   

0  | X | X | X | X |
   ----------------
1  | X | X | O | O |
   ----------------
2  | X | O | O | O |
   ----------------
3  | X | O | O | O |

 

Now I have to do win conditions and maybe try to make AI to play smart not random lol. Thank you for your help!!

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/page/2/#findComment-12066052
Share on other sites

Link to post
Share on other sites

public static void userTurn(Scanner console, char[][] board){
        
        size = board.length;
        
        System.out.println("Select row");

        int row = console.nextInt() - 1;
        
        System.out.println("Select column");
        
        int col = console.nextInt() - 1;
        
        if(row > size || col > size || row > size && col > size) {
            System.out.println("That doesn't fit on the board dummy!");
            userTurnErr(console, board);
        } else if(board[row][col] == ' '){
            board[row][col] = 'X';
            createBoard(board);
        } else {
            System.out.println("That space is already occupied");
          userTurnErr(console, board);
            
        }
        compTurn(console, board, circle, cross);
    }

 

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/page/2/#findComment-12066079
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×