Connect4 And More
Win Check Routine
old way:
for (i ----)
for (j ------)
if (board[i][j] == board[i][j+1])
New way:
if (hasWin(board,0,1) || hasWin(board,1,0) || hasWin(board,1,1) || hasWin(board,1,-1))
Game is Won! // this only applies if the hasWin func. returns a boolean.
// if you want to use the below func, change this to its own function.
char hasWin(char[][] board, int dx, int dy)
{
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
if (r+3*dy < 0 || r+3*dy >= board.length || c+3*dx < 0 || c+3*dx >= board[r].length) { continue; } // handles array out of bounds errors
if (board[r][c] != ' ' && board[r][c] == board[r+dy][c+dx]
&& board[r+2*dy][c+2*dx] == board[r][c]
&& board[r+3*dy][c+3*dy] == board[r][c]) {
return board[r][c]; // return winning player
}
}
}
return board ' '; // also, change this to false, and the above true.
}
Minesweeper Notes
J-Frame
- Expand JFrame out, and then insert the grid layout into the JFrame.
- Grid size:
Streams and File I/O
Creating a Text File
String fileName = "out.txt";
PrintWriter outputStream = null;
try {
outputStream = new PrintWriter(fileName);
}
catch { blarg.
Buffering and Closing
- PrintWriter "buffers" output
- Stores output so it can save it in large chunks
- Once we are done writing, we should explicitly close the stream with: outputStream.close();