| Title: | Play "Four in a Row" |
|---|---|
| Description: | Play or simulate games of "Four in a Row" in the R console. This package is designed for educational purposes, encouraging users to write their own functions to play the game automatically. It contains a collection of built-in functions that play the game at various skill levels, for users to test their own functions against. |
| Authors: | Kelly Street [aut, cre] (ORCID: <https://orcid.org/0000-0001-6379-5013>) |
| Maintainer: | Kelly Street <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-05-24 07:49:13 UTC |
| Source: | https://github.com/kstreet13/fourinarow |
Computer players that select their next move based on various amounts of internal logic.
randomBot(game) easyBot(game) mediumBot(game) hardBot(game)randomBot(game) easyBot(game) mediumBot(game) hardBot(game)
game |
A |
Returns an integer between 1 and 7. Each bot only selects from the set of valid moves, so they won't select a column that is already full.
randomBot(): Chooses moves randomly.
easyBot(): Tries to make 4 in a row, but does not consider its
opponents moves.
mediumBot(): Selects a move based on simple internal logic. It tries to
make 4 in a row and tries to block the opponent from winning, but does not
consider possible downstream moves.
hardBot(): Selects a move by looking three moves ahead (with downstream
moves selected by internal logic similar to mediumBot).
play4inaRow(randomBot, easyBot) play4inaRow(mediumBot, hardBot)play4inaRow(randomBot, easyBot) play4inaRow(mediumBot, hardBot)
Manually constructs a dataset containing all possible winning sets of four
in a standard (6x7) game of Four in a Row.
getSetsof4()getSetsof4()
A matrix with 69 rows and 4 columns. Each row contains numeric
indices for a potential winning set of four in a row. For example,
sets[1,] represents the set of four vertical spaces starting in the
top left corner.
getSetsof4()getSetsof4()
Get moves as input from the command line, allowing users to play against a bot.
humanPlayer(game)humanPlayer(game)
game |
A |
While possible, human vs. human games can be confusing because the
game switches Xs and Os between turns (so that every player
sees their own pieces as X).
Prints the current game board and prompts the user to input a move, which must be an integer between 1 and 7 and a valid move in the current game.
play4inaRow(humanPlayer, randomBot)play4inaRow(humanPlayer, randomBot)
Convenience function called internally by play4inaRow that switches
the player symbols, so that each player sees their own pieces as "X"
and their opponents' as "O".
invertPieces(game)invertPieces(game)
game |
A |
Returns the game matrix with "X" and "O"
symbols switched.
game <- matrix(sample(c('X','O','.'), 6*7, replace=TRUE), nrow = 6, ncol = 7) game invertPieces(game)game <- matrix(sample(c('X','O','.'), 6*7, replace=TRUE), nrow = 6, ncol = 7) game invertPieces(game)
Play a game of Four in a Row
play4inaRow(playerOne, playerTwo, verbose = TRUE)play4inaRow(playerOne, playerTwo, verbose = TRUE)
playerOne |
A function that takes the current board as input and returns the next move (1-7) for Player 1. |
playerTwo |
Same for Player 2. Note that both functions see their pieces as "X" and opponent's pieces as "O". |
verbose |
Logical value indicating whether or not to print the final
board to the console (default is |
The game is played on a grid and players alternate placing
markers in one of the columns. The piece will "fall" to the lowest
unoccupied space in that column. The game ends when one player wins by
getting four pieces in a row (horizontally, vertically, or diagonally).
Note that every player will see their markers as Xs when it is their turn.
Returns 1 or 2 to indicate whether Player 1 or Player
2 was the winner. Returns 0 in the case of a tie.
play4inaRow(randomBot, randomBot)play4inaRow(randomBot, randomBot)
This is a convenient way to test two bots against each other over a large number of games while ensuring that neither bot has an advantage from going first more often than the other.
testBots(playerOne, playerTwo, n = 100)testBots(playerOne, playerTwo, n = 100)
playerOne |
A function that takes the current board as input and returns the next move (1-7) for Player 1. |
playerTwo |
Same for Player 2. Note that both functions see their pieces as "X" and opponent's pieces as "O". |
n |
Numerical value of how many games to simulate. Must be an even number, so that both bots get the same number of games with the first move (default = 100). |
Returns a vector of counts for the number of ties, wins by
playerOne, and wins by playerTwo.
testBots(easyBot, randomBot)testBots(easyBot, randomBot)