Forum Discussion
ChatGPT AI - A new starting point for content creation?
So my first call in chatGPT was:
"javascript code to check for a win or loss in a connectfour game"
From that i got quite usefull code..
This was the result:
// Define the game board as a 2D arraylet board = [ [0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0]];
// Define the function to check for a winfunction checkForWin(player) {// Check horizontalfor (let row = 0; row < 6; row++) {for (let col = 0; col < 4; col++) {if (board[row][col] === player &&board[row][col+1] === player &&board[row][col+2] === player &&board[row][col+3] === player) {return true;}}}
// Check verticalfor (let row = 0; row < 3; row++) {for (let col = 0; col < 7; col++) {if (board[row][col] === player &&board[row+1][col] === player &&board[row+2][col] === player &&board[row+3][col] === player) {return true;}}}
// Check diagonalfor (let row = 0; row < 3; row++) {for (let col = 0; col < 4; col++) {if (board[row][col] === player &&board[row+1][col+1] === player &&board[row+2][col+2] === player &&board[row+3][col+3] === player) {return true;}}}
// Check other diagonalfor (let row = 3; row < 6; row++) {for (let col = 0; col < 4; col++) {if (board[row][col] === player &&board[row-1][col+1] === player &&board[row-2][col+2] === player &&board[row-3][col+3] === player) {return true;}}}
return false;}
// Usage:// To check if player 1 wins, call checkForWin(1)// To check if player 2 wins, call checkForWin(2)
This was the description added:
In this code, board is the 2D array that represents the Connect Four game board. The checkForWin function takes a player number as an argument (1 for player 1, 2 for player 2), and returns true if the player has won, or false otherwise.
The function checks for a win by iterating over the game board and looking for four consecutive pieces of the same color in a row, column, or diagonal. If it finds such a sequence, it returns true. If it iterates over the entire board and doesn't find a winning sequence, it returns false.
A few issues to get it working with my game. 'player', amount of rows/cols and the 'player' interfered with the Storyline player. But saved me quite some time this way.
Related Content
- 9 months ago
- 10 months ago
- 10 months ago