Note that there are some explanatory texts on larger screens.

plurals
  1. POArray programming - check winner in a Tic Tac Toe game for an nxn board with n players
    primarykey
    data
    text
    <p>I am making a tic tac toe game for n number of players on a nxn board, but the winning condition is aways 3 on a row. My so far solution to the problem is: when a move is made the program will check the following square for 3 on a row.</p> <pre><code>(x-1,y+1) (x,y+1) (x+1,y+1) (x-1,y) (x,y) (x+1,y) (x-1,y-1) (x,y-1) (x+1,y-1) </code></pre> <p>It will check the top (x-1,y+1) (x,y+1) (x+1,y+1) bottom(x-1,y-1) (x,y-1) (x+1,y-1) sides(x+1,y+1) (x+1,y) (x+1,y-1) , (x-1,y+1) (x-1,y) (x-1,y-1) , the diagonals and the ones going through the middle(x,y).</p> <p>my code so far is:</p> <pre><code> public int checkWinning() { for(int a = 1; a &lt; size-1; a++){ for(int b = 1; b &lt; size-1; b++){ if (board[a][b] == board[a+1][b] &amp;&amp; board[a][b] == board[a-1][b]){ return board[a][b]; }else if(board[a][b] == board[a][b+1] &amp;&amp; board[a][b] == board[a][b-1]){ return board[a][b]; }else if(board[a][b] == board[a+1][b-1] &amp;&amp; board[a][b] == board[a-1][b+1]){ return board[a][b]; }else if(board[a][b] == board[a+1][b+1] &amp;&amp; board[a][b] == board[a-1][b-1]){ return board[a][b]; } } } for(int d = 1; d &lt; size-1; d++){ if (board[0][d] == board[0][d-1] &amp;&amp; board[0][d] == board[0][d+1]){ return board[0][d]; } else if (board[size-1][d] == board[size-1][d-1] &amp;&amp; board[size-1][d] == board[size-1][d+1]){ return board[size-1][d]; } } for(int c = 1; c &lt; size-1; c++){ if (board[c][0] == board[c-1][0] &amp;&amp; board[c][0] == board[c+1][0]){ return board[c][0]; }else if(board[c][size-1] == board[c-1][size-1] &amp;&amp; board[c][size-1] == board[c+1][size-1]){ return board[c][size-1]; } } return 0; } </code></pre> <p>where the first section is where I check the ones through the middle and diagonals. the second section I check the top an bottom and the top and the thrid section checks the sides. </p> <p>When it returns 0 is means that there are no winner yet.</p> <pre><code>@override public void checkResult() { int winner = this.board.checkWinning(); if (winner &gt; 0) { this.ui.showResult("Player "+winner+" wins!"); } if (this.board.checkFull()) { this.ui.showResult("This is a DRAW!"); } } </code></pre> <p>Board[x][y] -> 2-dimensional array representing the board, The coordinates are counted from top-left (0,0) to bottom-right (size-1, size-1), board[x][y] == 0 signifies free at position (x,y), board[x][y] == i for i > 0 signifies that Player i made a move on (x,y), just so you know it.</p> <p>my problem is that when i expands the board to a size larger than 3x3 the program somehow overwrites it self or a does not check every thing sides top and bottom every time, and I can't seem too se why.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload