Alright, right now I am trying to create a basic four in a row game with a 10×10 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?
Why not using a multidimensional array to set coordinate and evaluate if has value or not:
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: