(Notice: This is a fairly satirical post. I find the services like chatGPT will actually speed up programming/solution building.)
Well, it's happening. Computers are writing code. I mean fairly complex code, not the simple control loops and conditionals we've seen in the past.
A bit ago, I wrote the game Chutes and Ladders, except that the computer plays all the roles (because the game is pretty much entirely random). It was a exercise I did for fun and it took about hour total.
Today I just asked a chatGPT to write the same program, and it did it in about 30 seconds...
Me:
Use Javascript to write the game Chutes and Ladders
OpenAI ChatGPT:
Here is an example of how you could write a JavaScript program to implement the game Chutes and Ladders:
// define the board size and the number of players
const BOARD_SIZE = 100;
const NUM_PLAYERS = 2;
// define the positions of the chutes and ladders on the board
const CHUTES = {
98: 78,
95: 75,
92: 88,
47: 26,
25: 7
};
const LADDERS = {
1: 38,
4: 14,
9: 31,
21: 42,
28: 84
};
// create an array of players and place each player at the starting position (1)
const players = Array(NUM_PLAYERS)
.fill()
.map((_, i) => ({ name: `Player ${i + 1}`, position: 1 }));
// play the game until one of the players reaches the end of the board
while (true) {
for (const player of players) {
// roll the dice and move the player's piece
const roll = Math.floor(Math.random() * 6) + 1;
player.position += roll;
console.log(`${player.name} rolls a ${roll} and moves to position ${player.position}`);
// if the player lands on a chute or ladder, move them to the new position
if (player.position in CHUTES) {
console.log(
`${player.name} landed on a chute and falls to position ${CHUTES[player.position]}`
);
player.position = CHUTES[player.position];
} else if (player.position in LADDERS) {
console.log(
`${player.name} climbed a ladder and moves to position ${LADDERS[player.position]}`
);
player.position = LADDERS[player.position];
}
// if the player reaches the end of the board, announce the winner and end the game
if (player.position >= BOARD_SIZE) {
console.log(`${player.name} wins!`);
process.exit();
}
}
}
This code will simulate a game of Chutes and Ladders for two players. Each player will take turns rolling a die and moving their piece on the board according to the roll. If a player lands on a chute or ladder, they will be moved to the corresponding position. The game will continue until one of the players reaches the end of the board and wins the game.
You can adjust the parameters of the game, such as the board size, the number of players, and the positions of the chutes and ladders, to change the behavior of the game. You can also add additional features, such as a graphical user interface or support for more players, to make the game more interactive and fun.
Me:
:O
gg. Game over. It's been fun.