https://en.wikipedia.org/wiki/Connect_Four
Does anyone remember the 2 player game connect 4? For those who don't it was a 6x7 board that stands vertical on a surface. The goal of connect 4 is to, well connect 4! The connection is counted if it is horizontal, diagonal, or vertical. You place your pieces on the board by inserting a piece at the top of a column where it falls to the bottom of that column. Our rules change 3 things in connect 4.
- Change #1 Winning is defined as the player with the most points. You get points by connecting 4 like in the rules - more on that later.
- Change #2 You have 3 players each round.
- Change #3 The board size is 9x9.
Scoring:
Score is based on how many you get in a row. If you have a 4 in a row group you get 1 point. If you have a 5 in a row group you get 2 points, 6 in a row 3 and so on.
Examples:
Note o
and x
are replaced with #
and ~
respectively, for better contrast
Example of empty board: (all examples are 2 player standard size board)
a b c d e f g
6 | | | | | | | |
5 | | | | | | | |
4 | | | | | | | |
3 | | | | | | | |
2 | | | | | | | |
1 |_|_|_|_|_|_|_|
If we drop a piece in coll d
, it will land in location1d
.
a b c d e f g
6 | | | | | | | |
5 | | | | | | | |
4 | | | | | | | |
3 | | | | | | | |
2 | | | | | | | |
1 |_|_|_|#|_|_|_|
If we now drop a piece in coll d
again, it will land in location 2d
. Here are examples of 4 in a row positions:
a b c d e f g
6 | | | | | | | |
5 | | | | | | | |
4 | | | |~| | | |
3 | | |~|#| | | |
2 | |~|#|~| |#| |
1 |~|#|~|#|_|#|_|
In this case x
gets 1 point diagonally (1a 2b 3c 4d
).
a b c d e f g
6 | | | | | | | |
5 | | | | | | | |
4 | | | |#| | | |
3 | | | |#| | | |
2 | | | |#| | | |
1 |_|~|_|#|~|_|~|
In this case, o
gets 1 point vertically (1d 2d 3d 4d
).
a b c d e f g
6 | | | | | | | |
5 | | | | | | | |
4 | | | | | | | |
3 | | | | | | | |
2 | | |#|#|#|#| |
1 |_|_|~|~|~|~|~|
In this case o
gets 2 points horizontally (1c 1d 1e 1f 1g
) and x
gets 1 point horizontally (2c 2d 2e 2f
).
a b c d e f g
6 | | |#| | | | |
5 | | |#| | | | |
4 | | |#| | | | |
3 | | |#| | |~| |
2 |~| |#| | |#|~|
1 |~|_|#|~| |~|~|
This time x
gets 3 points for a 6 in a row (1c 2c 3c 4c 5c 6c
).
Input / Output
You will have access to the board via a 2d array. Each location will be represented with an int
representing a player id. You will also have your player id passed to your function. You make your move by returning which coll you want to drop your piece into. Each round 3 players will be chosen to play. At the end of the game, all players will have played an even amount of games.
For the moment 100k rounds will be run (note this takes a long time, you may want to reduce it for fast turnaround testing). Overall the winner is the player with the most wins.
The controller can be found here: https://github.com/JJ-Atkinson/Connect-n/tree/master.
Writing a bot:
To write a bot you must extend the Player
class. Player
is abstract and has one method to implement, int makeMove(void)
. In makeMove
you will decide which coll you would like to drop your piece into. If you chose an invalid coll (e.g. coll does not exist, coll is filled already), your turn will be skipped. In the Player
class you have many useful helper methods. A listing of the most important ones follows:
boolean ensureValidMove(int coll)
: Return true if the coll is on the board and the coll is not filled yet.int[] getBoardSize()
: Return a int array where[0]
is the number of columns, and[1]
is the number of rows.int[][] getBoard()
: Return a copy of the board. You should access it like this:[coll number][row number from bottom]
.- To find the rest, look at the
Player
class. EMPTY_CELL
: The value of an empty cell
Since this will be multi-threaded, I have also included a random
function if you need it.
Debugging your bot:
I have included some things in the controller to make it simpler to debug a bot. The first one is Runner#SHOW_STATISTICS
. If this is enabled, you will see a printout of player groups played, including a count of bot wins. Example:
OnePlayBot, PackingBot, BuggyBot,
OnePlayBot -> 6
PackingBot -> 5
BuggyBot -> 3
Draw -> 1
You can also make a custom game with the connectn.game.CustomGame
class, you can see the scores and winner of each round. You can even add yourself to the mix with UserBot
.
Adding your bot:
To add your bot to the lineup, go to the PlayerFactory
static block and add the following line:
playerCreator.put(MyBot.class, MyBot::new);
Other things to note:
- The simulations are multi-threaded. If you want to turn that off, go to
Runner#runGames()
and comment this line (.parallel()
). - To change the number of games, set
Runner#MINIMUM_NUMBER_OF_GAMES
to your liking.
Added later:
- Communication among bots is disallowed.
Related: Play Connect 4!
================================
Scoreboard: (100 000 games)
MaxGayne -> 22662
RowBot -> 17884
OnePlayBot -> 10354
JealousBot -> 10140
Progressive -> 7965
Draw -> 7553
StraightForwardBot -> 7542
RandomBot -> 6700
PackingBot -> 5317
BasicBlockBot -> 1282
BuggyBot -> 1114
FairDiceRoll -> 853
Steve -> 634
================================
Player
class to see all methods available. \$\endgroup\$ensureValidMove
(unless your strategy is to pass this turn of course). \$\endgroup\$