Note that there are some explanatory texts on larger screens.

plurals
  1. POArray programming - Tict Tac Toe nxn board for n players
    primarykey
    data
    text
    <p>This Piece of code is a part a of a larger project that makes a ti tac toe game for n players on a nxn board, it represents the board of the tic tac toe game. </p> <pre><code>/** represents a tic tac toe board of a given size */ public class TTTBoard { /** 2-dimensional array representing the board * 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 &gt; 0 signifies that Player i made a move on (x,y) */ private int[][] board; /** size of the (quadratic) board */ private int size; /** This private static integer i used to make a counter */ private static int movecount = 0; /** constructor for creating a copy of the board * not needed in Part 1 - can be viewed as an example */ public TTTBoard(TTTBoard original) { this.size = original.size; for (int y = 0; y &lt; this.size; y++) { for (int x = 0; x &lt; this.size; x++) { this.board[x][y] = original.board[x][y]; } } } /** constructor for creating an empty board for a given number of players */ public TTTBoard(int numPlayers) { this.size = numPlayers+1; this.board = new int[this.getSize()][this.getSize()]; } /** checks whether the board is free at the given position */ public boolean isFree(Coordinate c) { if (board[c.getX()][c.getY()] == 0) { return true; } else { return false; } } /** returns the player that made a move on (x,y) or 0 if the positon is free */ public int getPlayer(Coordinate c) { if (isFree(c) == true){ return board[c.getX()][c.getY()]; }else{ return 0; } } /** record that a given player made a move at the given position * checks that the given positions is on the board * checks that the player number is valid */ public void addMove(Coordinate c, int player) { if (c.checkBoundaries(size, size) == true &amp;&amp; isFree(c) == true &amp;&amp; player &gt;= 0 &amp;&amp; player &lt;= size-1){ board[c.getX()][c.getY()] = player; movecount++; } else { throw new IllegalArgumentException("Ivalid move"); } } /** returns true if, and only if, there are no more free positions on the board */ public boolean checkFull() { if (movecount == size*size) { return true; } else { return false; } } /** returns 0 if no player has won (yet) * otherwise returns the number of the player that has three in a row */ public int checkWinning() { int b = 1; for(int a = 1; a &lt; size-1; a++){ 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 a = 1; a &lt; size-1; a++){ if (board[b][a] == board[b+1][a] &amp;&amp; board[b][a] == board[b-1][a]){ return board[b][a]; }else if(board[b][a] == board[b][a+1] &amp;&amp; board[b][a] == board[b][a-1]){ return board[b][a]; }else if(board[b][a] == board[b+1][a-1] &amp;&amp; board[b][a] == board[b-1][a+1]){ return board[b][a]; }else if(board[b][a] == board[b+1][a+1] &amp;&amp; board[b][a] == board[b-1][a-1]){ return board[b][a]; } } return 0; } /** getter for size of the board */ public int getSize() { return this.size; } } </code></pre> <p>As my test of running the hole program shows that there are a problem with the addMove, why I know that is because that I get no error massages but the program does not do what I what it too:</p> <pre><code>public void addMove(Coordinate c, int player) { if (c.checkBoundaries(size, size) == true &amp;&amp; isFree(c) == true &amp;&amp; player &gt;= 0 &amp;&amp; player &lt;= size-1){ board[c.getX()][c.getY()] = player; movecount++; } else { throw new IllegalArgumentException("dumpass!"); } } </code></pre> <p>I looks like that the code does not record the moves right, and I cannot see what I do wrong here. x and y coordinates i the board comes from another class Coordinate, and thats why a am using getters to get them.</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.
 

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