Referencing a match statement when populating an array [closed]Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?PHP: Delete an element from an arrayHow to insert an item into an array at a specific index (JavaScript)?How do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?
How to source a part of a file
Revoked SSL certificate
Horror movie about a virus at the prom; beginning and end are stylized as a cartoon
Paid for article while in US on F-1 visa?
dbcc cleantable batch size explanation
Does detail obscure or enhance action?
How is it possible to have an ability score that is less than 3?
What's that red-plus icon near a text?
I'm flying to France today and my passport expires in less than 2 months
What does it mean to describe someone as a butt steak?
Can I make popcorn with any corn?
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
Why is 150k or 200k jobs considered good when there's 300k+ births a month?
Perform and show arithmetic with LuaLaTeX
How does one intimidate enemies without having the capacity for violence?
What would happen to a modern skyscraper if it rains micro blackholes?
Java Casting: Java 11 throws LambdaConversionException while 1.8 does not
What is the word for reserving something for yourself before others do?
Modeling an IP Address
How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?
What defenses are there against being summoned by the Gate spell?
How does quantile regression compare to logistic regression with the variable split at the quantile?
What doth I be?
Important Resources for Dark Age Civilizations?
Referencing a match statement when populating an array [closed]
Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?PHP: Delete an element from an arrayHow to insert an item into an array at a specific index (JavaScript)?How do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have been set a simple task to create a TicTacToe game, using arrays.
I have a class 'Player' with the objects 'X' and 'O'
I have the following Match statement:
def optPlayerToChar(p: Option[Player]): Char = p match
case None => ' '
case Some(O) => 'O'
case Some(X) => 'X'
I have the following declaration for the grid:
private var grid: Array[Array[Option[Player]]] = ofDim[Option[Player]](3,3)
I need to be able to populate the grid at (x)(y) using the match statement. I have tried many things but can't seem to get it to work for some reason. Apologies if it is just a simple solution I am overlooking.
it needs to be in a format like the following but obviously functioning:
grid(x)(y) = optPlayerToChar(p: Option[Player])
the code I am using to show the grid (in case you need it):
def showGrid()
println()
for(j <- 0 to 2) " + optPlayerToChar(grid(2)(j)))
println()
if (j<2) println(" --- --- --- ")
println()
arrays scala
closed as unclear what you're asking by Dima, Shaido, Tiw, Dale Burrell, Matteo Baldi Mar 8 at 8:52
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have been set a simple task to create a TicTacToe game, using arrays.
I have a class 'Player' with the objects 'X' and 'O'
I have the following Match statement:
def optPlayerToChar(p: Option[Player]): Char = p match
case None => ' '
case Some(O) => 'O'
case Some(X) => 'X'
I have the following declaration for the grid:
private var grid: Array[Array[Option[Player]]] = ofDim[Option[Player]](3,3)
I need to be able to populate the grid at (x)(y) using the match statement. I have tried many things but can't seem to get it to work for some reason. Apologies if it is just a simple solution I am overlooking.
it needs to be in a format like the following but obviously functioning:
grid(x)(y) = optPlayerToChar(p: Option[Player])
the code I am using to show the grid (in case you need it):
def showGrid()
println()
for(j <- 0 to 2) " + optPlayerToChar(grid(2)(j)))
println()
if (j<2) println(" --- --- --- ")
println()
arrays scala
closed as unclear what you're asking by Dima, Shaido, Tiw, Dale Burrell, Matteo Baldi Mar 8 at 8:52
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
So, what's the actual problem?
– Dima
Mar 8 at 1:57
add a comment |
I have been set a simple task to create a TicTacToe game, using arrays.
I have a class 'Player' with the objects 'X' and 'O'
I have the following Match statement:
def optPlayerToChar(p: Option[Player]): Char = p match
case None => ' '
case Some(O) => 'O'
case Some(X) => 'X'
I have the following declaration for the grid:
private var grid: Array[Array[Option[Player]]] = ofDim[Option[Player]](3,3)
I need to be able to populate the grid at (x)(y) using the match statement. I have tried many things but can't seem to get it to work for some reason. Apologies if it is just a simple solution I am overlooking.
it needs to be in a format like the following but obviously functioning:
grid(x)(y) = optPlayerToChar(p: Option[Player])
the code I am using to show the grid (in case you need it):
def showGrid()
println()
for(j <- 0 to 2) " + optPlayerToChar(grid(2)(j)))
println()
if (j<2) println(" --- --- --- ")
println()
arrays scala
I have been set a simple task to create a TicTacToe game, using arrays.
I have a class 'Player' with the objects 'X' and 'O'
I have the following Match statement:
def optPlayerToChar(p: Option[Player]): Char = p match
case None => ' '
case Some(O) => 'O'
case Some(X) => 'X'
I have the following declaration for the grid:
private var grid: Array[Array[Option[Player]]] = ofDim[Option[Player]](3,3)
I need to be able to populate the grid at (x)(y) using the match statement. I have tried many things but can't seem to get it to work for some reason. Apologies if it is just a simple solution I am overlooking.
it needs to be in a format like the following but obviously functioning:
grid(x)(y) = optPlayerToChar(p: Option[Player])
the code I am using to show the grid (in case you need it):
def showGrid()
println()
for(j <- 0 to 2) " + optPlayerToChar(grid(2)(j)))
println()
if (j<2) println(" --- --- --- ")
println()
arrays scala
arrays scala
asked Mar 8 at 1:47
Frazer EastmanFrazer Eastman
365
365
closed as unclear what you're asking by Dima, Shaido, Tiw, Dale Burrell, Matteo Baldi Mar 8 at 8:52
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Dima, Shaido, Tiw, Dale Burrell, Matteo Baldi Mar 8 at 8:52
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
So, what's the actual problem?
– Dima
Mar 8 at 1:57
add a comment |
2
So, what's the actual problem?
– Dima
Mar 8 at 1:57
2
2
So, what's the actual problem?
– Dima
Mar 8 at 1:57
So, what's the actual problem?
– Dima
Mar 8 at 1:57
add a comment |
1 Answer
1
active
oldest
votes
You define your grid but you don't populate it, so it is populated with null by default. null isn't the same thing as None.
val grid: Array[Array[Option[Player]]] =
Array.fill[Option[Player]](3,3)(None)
With this, optPlayerToChar() and showGrid() should work as planned.
This won't work grid(x)(y) = optPlayerToChar(p: Option[Player]) because each grid element is an Option[Player] but optPlayerToChar() returns a Char. Those are, of course, unrelated and incompatible types.
Note: There's no reason to make grid a var. Arrays are mutable. The variable usually doesn't need to be mutable as well.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You define your grid but you don't populate it, so it is populated with null by default. null isn't the same thing as None.
val grid: Array[Array[Option[Player]]] =
Array.fill[Option[Player]](3,3)(None)
With this, optPlayerToChar() and showGrid() should work as planned.
This won't work grid(x)(y) = optPlayerToChar(p: Option[Player]) because each grid element is an Option[Player] but optPlayerToChar() returns a Char. Those are, of course, unrelated and incompatible types.
Note: There's no reason to make grid a var. Arrays are mutable. The variable usually doesn't need to be mutable as well.
add a comment |
You define your grid but you don't populate it, so it is populated with null by default. null isn't the same thing as None.
val grid: Array[Array[Option[Player]]] =
Array.fill[Option[Player]](3,3)(None)
With this, optPlayerToChar() and showGrid() should work as planned.
This won't work grid(x)(y) = optPlayerToChar(p: Option[Player]) because each grid element is an Option[Player] but optPlayerToChar() returns a Char. Those are, of course, unrelated and incompatible types.
Note: There's no reason to make grid a var. Arrays are mutable. The variable usually doesn't need to be mutable as well.
add a comment |
You define your grid but you don't populate it, so it is populated with null by default. null isn't the same thing as None.
val grid: Array[Array[Option[Player]]] =
Array.fill[Option[Player]](3,3)(None)
With this, optPlayerToChar() and showGrid() should work as planned.
This won't work grid(x)(y) = optPlayerToChar(p: Option[Player]) because each grid element is an Option[Player] but optPlayerToChar() returns a Char. Those are, of course, unrelated and incompatible types.
Note: There's no reason to make grid a var. Arrays are mutable. The variable usually doesn't need to be mutable as well.
You define your grid but you don't populate it, so it is populated with null by default. null isn't the same thing as None.
val grid: Array[Array[Option[Player]]] =
Array.fill[Option[Player]](3,3)(None)
With this, optPlayerToChar() and showGrid() should work as planned.
This won't work grid(x)(y) = optPlayerToChar(p: Option[Player]) because each grid element is an Option[Player] but optPlayerToChar() returns a Char. Those are, of course, unrelated and incompatible types.
Note: There's no reason to make grid a var. Arrays are mutable. The variable usually doesn't need to be mutable as well.
edited Mar 8 at 8:14
answered Mar 8 at 7:06
jwvhjwvh
28.5k52141
28.5k52141
add a comment |
add a comment |
2
So, what's the actual problem?
– Dima
Mar 8 at 1:57