Four in a row game

by SiemHermans at 2013-01-15 08:48:04

Alright, right now I am trying to create a basic four in a row game with a 10x10 board. (Ranging from A to J and 1 to 10.) Now, the goal of the game is to prohibit the player from entering a ‘coordinate’ that has already been used, and the coordinate can’t ‘float’.

For example: When the player chooses A9, but A1 to A8 are still empty, the choice can’t be there. (As if you drop a coin down the collumn)

What I wanted to ask is, how do I validate that the previous value isn’t in use? I was thinking about arrays, but I don’t know how to make that work.

This is the code I wrote to validat the coordinate:

Function Validate-Move{

do{
do {
$script:coordinateLetter = Read-host "Please enter a letter from A to J"
if($script:coordinateLetter.Length -gt 1){}
}
while ($script:coordinateLetter.Length -gt 1)
}
until($script:coordinateLetter -cmatch "[A-J]")

do{
$script:coordinateNumber = Read-host "Please enter a number from 1 to 10!"

}
until($script:coordinateNumber -match "^([1-9]|[1][0])$")


}
$playerMove = $coordinateLetter + $coordinateNumber
Write-host "You have chosen " -NoNewline; Write-host "$fullCoordinate" -for green

After this block of code I would like to perform a check that the value is possible for use. Jerry Lee Ford’s book uses the following method in the Three in a Row game:
if (($move -eq "A1") -and ($A1 -ne " ")) {$result = "Invalid"}
if (($move -eq "A2") -and ($A2 -ne " ")) {$result = "Invalid"}
if (($move -eq "A3") -and ($A3 -ne " ")) {$result = "Invalid"}
if (($move -eq "B1") -and ($B1 -ne " ")) {$result = "Invalid"}

and so forth. But that would mean that I would have to make 100 variables (!) and write a check for all of them ?

Can anyone point me in the right direction?
by nohandle at 2013-01-15 10:06:30
Isn’t it enough to specify just the one coordinate because the disk falls down to the availible spot?

Checking every move possible throught set of conditions is really not the way :slight_smile: Using the array is a good start.
But before caring about how exactly you are going to implement it take a list of paper and figure out how the game works. Write down everything you think of. Create the algorithm on the paper. And then implement it.

If you don’t want to figure it yourself there are lot of tested implementations on the internet (I won’t link because I don’t want to spoil it for you). :slight_smile:
by SiemHermans at 2013-01-15 10:16:52
Well I’ve been at it for ages, and the way I see it is that I need to validate the coordinate for starters, then check wether the coordinate is already being used, then check wether the underlying coordinates are being used, and then write it to the array. After that the turn goes to the other player.

A guy on Stackoverflow told me to use a multidimensional array and check it like this:

$m = New-Object 'object[,]' 10,10
$m[0,0] = "x"
$m[0,9] = "x"

[bool]$m[0,2]
False

[bool]$m[0,1]
True
You need to map the range A - J to 0 - 9 'cause array are 0-based

after the user input you can check if slot is busy like this:

if ([bool]$m=[$coordinateLetter,$coordinateNumber]) #if busy
{ do this stuff…}
else
{ do something other… }


Not completely sure what he meant tho. And please if you wouldn’t mind sharing the links.
by nohandle at 2013-01-15 10:43:34
hello again,
if your goal is to implement it yourself I wouldn’t follow the link, but here it is http://www.pomakis.com/c4/.

Arrays are very likely the way you end up doing it, but try to forget any way you tried to implement it so far. Abstract from the AtoJs, zeros and object arrays and just describe the problem in plain english. Using non special terms like yellow and red disks or whatever the thingies are called. Until you are clear on how it should work on the "high level" digging into the arrays is just lot of frustrating work.

What happens if you drop the disk into one of the slots? what is does? how deep it can fall? can it move to sides? do you have to care about the other slots in the game? :slight_smile:
by SiemHermans at 2013-01-16 08:58:35
I have decided to try it on my own either way. I am running into the following problem however:


I am creating a four in a row game in which you get a grid of 10x10. The point is that my Write-host created grid jumps around because it isn’t filled with any empty strings. My code looks like this:

$CreateBoard = New-object "Array[,]" 10,10

Function Add-ToColumn{
param ([Int] $columnnum,[String] $player)

PROCESS{if (0..9 -notcontains $columnnum){"Invalid move";return}
#0 is the bottom, 9 is the top
for($i = 0; $i -le 9; $i++)
{
if ($CreateBoard[$columnnum, $i] -eq $null)
{
$CreateBoard[$columnnum, $i] = $player
"Coin placed in $columnnum, $i coins in the column!"
return
}

}
#if you get here, column is full
"Invalid move"
}
}
Add-ToColumn 1 x
Add-ToColumn 4 o
Add-ToColumn 3 x
Add-ToColumn 1 x


Write-host "$($CreateBoard[1,0])"


Write-host " 1 2 3 4 5 6 7 8 9 10 "
Write-host "±–±–±–±–±–±–±–±–±–±–+ "
Write-host "| $($CreateBoard[1,9]) | $($CreateBoard[2,9]) | $($CreateBoard[3,9]) | $($CreateBoard[4,9]) | $($CreateBoard[5,9]) | $($CreateBoard[6,9]) | $($CreateBoard[7,9]) | $($CreateBoard[8,9]) | $($CreateBoard[9,9]) | $($CreateBoard[10,9]) | A"
Write-host "±–±–±–±–±–±–±–±–±–±–+ "
Write-host "| $($CreateBoard[1,8]) | $($CreateBoard[2,8]) | $($CreateBoard[3,8]) | $($CreateBoard[4,8]) | $($CreateBoard[5,8]) | $($CreateBoard[6,8]) | $($CreateBoard[7,8]) | $($CreateBoard[8,8]) | $($CreateBoard[9,8]) | $($CreateBoard[10,8]) | B"
Write-host "±–±–±–±–±–±–±–±–±–±–+ "
Write-host "| $($CreateBoard[1,7]) | $($CreateBoard[2,7]) | $($CreateBoard[3,7]) | $($CreateBoard[4,7]) | $($CreateBoard[5,7]) | $($CreateBoard[6,7]) | $($CreateBoard[7,7]) | $($CreateBoard[8,7]) | $($CreateBoard[9,7]) | $($CreateBoard[10,7]) | C"
Write-host "±–±–±–±–±–±–±–±–±–±–+ "
Write-host "| $($CreateBoard[1,6]) | $($CreateBoard[2,6]) | $($CreateBoard[3,6]) | $($CreateBoard[4,6]) | $($CreateBoard[5,6]) | $($CreateBoard[6,6]) | $($CreateBoard[7,6]) | $($CreateBoard[8,6]) | $($CreateBoard[9,6]) | $($CreateBoard[10,6]) | D"
Write-host "±–±–±–±–±–±–±–±–±–±–+ "
Write-host "| $($CreateBoard[1,5]) | $($CreateBoard[2,5]) | $($CreateBoard[3,5]) | $($CreateBoard[4,5]) | $($CreateBoard[5,5]) | $($CreateBoard[6,5]) | $($CreateBoard[7,5]) | $($CreateBoard[8,5]) | $($CreateBoard[9,5]) | $($CreateBoard[10,5]) | E"
Write-host "±–±–±–±–±–±–±–±–±–±–+ "
Write-host "| $($CreateBoard[1,4]) | $($CreateBoard[2,4]) | $($CreateBoard[3,4]) | $($CreateBoard[4,4]) | $($CreateBoard[5,4]) | $($CreateBoard[6,4]) | $($CreateBoard[7,4]) | $($CreateBoard[8,4]) | $($CreateBoard[9,4]) | $($CreateBoard[10,4]) | F"
Write-host "±–±–±–±–±–±–±–±–±–±–+ "
Write-host "| $($CreateBoard[1,3]) | $($CreateBoard[2,3]) | $($CreateBoard[3,3]) | $($CreateBoard[4,3]) | $($CreateBoard[5,3]) | $($CreateBoard[6,3]) | $($CreateBoard[7,3]) | $($CreateBoard[8,3]) | $($CreateBoard[9,3]) | $($CreateBoard[10,3]) | G"
Write-host "±–±–±–±–±–±–±–±–±–±–+ "
Write-host "| $($CreateBoard[1,2]) | $($CreateBoard[2,2]) | $($CreateBoard[3,2]) | $($CreateBoard[4,2]) | $($CreateBoard[5,2]) | $($CreateBoard[6,2]) | $($CreateBoard[7,2]) | $($CreateBoard[8,2]) | $($CreateBoard[9,2]) | $($CreateBoard[10,2]) | H"
Write-host "±–±–±–±–±–±–±–±–±–±–+ "
Write-host "| $($CreateBoard[1,1]) | $($CreateBoard[2,1]) | $($CreateBoard[3,1]) | $($CreateBoard[4,1]) | $($CreateBoard[5,1]) | $($CreateBoard[6,1]) | $($CreateBoard[7,1]) | $($CreateBoard[8,1]) | $($CreateBoard[9,1]) | $($CreateBoard[10,1]) | I"
Write-host "±–±–±–±–±–±–±–±–±–±–+ "
Write-host "| $($CreateBoard[1,0]) | $($CreateBoard[2,0]) | $($CreateBoard[3,0]) | $($CreateBoard[4,0]) | $($CreateBoard[5,0]) | $($CreateBoard[6,0]) | $($CreateBoard[7,0]) | $($CreateBoard[8,0]) | $($CreateBoard[9,0]) | $($CreateBoard[10,0]) | J"
Write-host "±–±–±–±–±–±–±–±–±–±–+ "

read-host


The output looks like this:

1 2 3 4 5 6 7 8 9 10
±–±–±–±–±–±–±–±–±–±–+
| | | | | | | | | | | A
±–±–±–±–±–±–±–±–±–±–+
| | | | | | | | | | | B
±–±–±–±–±–±–±–±–±–±–+
| | | | | | | | | | | C
±–±–±–±–±–±–±–±–±–±–+
| | | | | | | | | | | D
±–±–±–±–±–±–±–±–±–±–+
| | | | | | | | | | | E
±–±–±–±–±–±–±–±–±–±–+
| | | | | | | | | | | F
±–±–±–±–±–±–±–±–±–±–+
| | | | | | | | | | | G
±–±–±–±–±–±–±–±–±–±–+
| | | | | | | | | | | H
±–±–±–±–±–±–±–±–±–±–+
| x | | | | | | | | | | I
±–±–±–±–±–±–±–±–±–±–+
| x | | x | o | | | | | | | J
±–±–±–±–±–±–±–±–±–±–+


What I want to achieve is to fill every spot in the array with a (" ") string to space it evenly. Is this possible at all?
by nohandle at 2013-01-16 09:20:03
Yes, just assign the " " to every item in the array.
I think you should consider generating the graphics instead of writing them by hand. :slight_smile:
by SiemHermans at 2013-01-16 09:45:01
Generating graphics? Could you perhaps elaborate ?
by nohandle at 2013-01-16 09:59:46
I did not mean anything special just creating function(s) tha will generate the grid for you in case you decide to change the size from 10 to 5.
by nohandle at 2013-01-16 10:04:43
I did not mean anything special just creating function(s) tha will generate the grid for you in case you decide to change the size from 10 to 5.
by SiemHermans at 2013-01-17 08:04:51
Alright I figured all that out. The array is filled, it switches between players and fills properly. Now my problem is: Is there an efficient way to search the array for a four in a row without having to make millions of if statements ? (It would have to be horizontal, vertical and diagonal).
If you perhaps have any terms I could search for that would be very helpful!

Really appreciate the help so far!
by nohandle at 2013-01-17 08:29:58
Using loops (and probably recursion, here i describe the concepts http://powershell.cz/2012/12/28/loop-versus-recursion/) you check the sorroundings of the disk to see if there are disks of the appropriate color. It is up to you to invent the solution. :slight_smile: