This question already has an answer here:
- Connect-n time! 12 answers
Four-In-A-Row AI Tournament
Today, we will be playing a Four-In-A-Row tournament. However, it is not us humans playing... instead, we must have our computers battle instead! This challenge is completely in C#.
Basic Game Rules
If you have never played, there are simple rules. 2 players take turns putting pieces into a 6x7 grid. The player chooses one of the 7 holes at the top of the grid, and puts a piece in. Gravity pulls the piece down, until it hits another piece underneath it.
The game is won when a player gets four of their pieces in a straight line, either vertical or horizontal. Diagonal lines do not count in this challenge, as I was lazy when programming my simulator. This will also make it easier for you to code your AIs.
You will write code that goes into a function that gets called when it is your turn. You can receive an argument,
states[,] board where
states is an enum with
defender, attacker, empty. This represents the occupation of a space. Your method must return an int.
The game ends when someone gets a four-in-a-row, or makes an illegal move. An illegal move is when your program returns an int that is 1) off the board (< 0 or > 6) or in a column that is completely filled. In the case of an illegal move, the opponent immediately wins.
Additionally, if your program exits the function or the program, you automatically lose the match. If your program causes a runtime error, you lose the match. You are allowed to try to make the other program cause a runtime error.
You are not allowed to access any variables, properties, or run any methods outside of your function's scope. You are not allowed to define any variables outside of your function's scope.
One exception is the
Dictionary<string, object> names
variables I have provided for your convenience. It is in the class
Programs, so it will keep it's data for the whole match. However, the opponent uses the same dictionary to save its data. One strategy is to sabotage the enemy program by modifying the data it uses.
Closing the Loopholes
Your submission may not make any external requests, for example to an online API. Your submission may not include a library that is made to solve four-in-a-row games. As a rule of thumb, your program shouldn't need to use anything except
system or anything included in the
You may not exploit any loopholes, either stated or implied in the rules, unless you have asked me through comments and I agreed.
This is a king-of-the-hill tournament, so the AI will be competing against each other. I will post the first answer. The next person will have to beat my program. To beat a program, you must never lose to it. You are only required to beat the program right before you. This process goes on until an answer has not been superseded after one week.
You may answer as many times as you want, but you may not counter your own program.
You may make edits to your code until it has been superseded.
In a nutshell, you must write a program that never loses to its predecessor.
Click here to see my match simulation. Paste in your code in the
Programs.cs file, in your corresponding function. The attacker is the one who is trying to supersede a program. The defender is the one whos program is attempting to hold its place. The defender always moves first.
After your program has been pasted in, run it. The console will open, and it will run the simulation.
Your submission must be in C#. Your submission must compile correctly. Your submission must follow this format:
#<answer number> [Previous](<Permalink to previous answer>) CODE HERE CODE HERE
Basically, you will be coding AIs to compete in four-in-a-row. However, read the whole thing if you want to compete!