Placing an apple in snake game written in Scala - scala

To place an apple in an empty place, I'm using the emptySpots() method to locate the points of the cells that are empty or free.
When I implement the code below, my game runs fine:
def emptySpots() : Point = {
val emptyPointsList = new ArrayBuffer[Point]()
var emptyLocation : Point = new Point(0,0)
emptyLocation = Point(random.randomInt(upTo = 25), random.randomInt(upTo = 25))
emptyLocation
}
However, when I try to implement the code below, it shows me errors:
def emptySpots() : Point = {
val emptyPointsList = new ArrayBuffer[Point]()
var emptyLocation : Point = new Point(0,0)
for(i <- 0 until gridDims.height) {
for(j <- 0 until gridDims.width) {
if(getCellType(Point(i, j)) == Empty()) {
emptyPointsList += Point(i, j)
}
}
}
if(!emptyPointsList.isEmpty) {
emptyLocation = emptyPointsList(random.randomInt(emptyPointsList.size))
}
emptyLocation
}
The errors are:
I would appreciate if anyone could point out what's wrong in my second implementation to show me such errors where my first implementation works fine.
P.S. The randomInt() is not wrong here because it's supported by the imported package and library

Related

Runtime Error - Google Code Jam 2019 Qualifier - Scala

I am attempting to submit a solution to Foregone Solution in the 2019 Google Code Jam Qualification Round Archives. I am new to Scala. My code runs to completion through IntelliJ. Is anybody aware of what may be causing a Runtime Error when I submit the below code to the Google Code Jam server?
import scala.io.StdIn
object main extends App {
val T = StdIn.readInt
var firstCheck: String = ""
var secondCheck: String = ""
var N: String = ""
for (i <- 1 to T) {
N = StdIn.readLine()
for (j <- 0 until N.length) {
if (N(j) == '4') secondCheck += "1"
else if (secondCheck != "") secondCheck += "0"
}
firstCheck = N.replaceAll("[4]", "3")
println(s"Case #$i: $firstCheck $secondCheck")
secondCheck = ""
}
}

Lexical Analyzer not getting the next character

So I am working on a project where we are making a small compiler program but before I can move on to the other parts I am having troubles with getting the lexical analyzer to output anything after '\BEGIN' afterwards I debugged it and it seems the value is stuck in a loop where the condition is saying the next character is always a newline. Is it because I haven't added the pattern matching yet to the defined tokens?
Here is the code
import java.util
//import com.sun.javafx.fxml.expression.Expression.Parser.Token
/*Lexical analyzer will be responsible for the following:
- finds the lexemes
- Checks each given character determining the tokens
* */
class MyLexicalAnalyzer extends LexicalAnalyzer {
//Array full of the keywords
//val SpecialCharacters = List(']', '#', '*', '+', '\\', '[', '(',')', "![", '=')
val TEXT = "[a-z] | _ | 0-9 | [A-Z]:"
private var sourceLine: String = null
private val lexeme: Array[Char] = new Array[Char](999)
private var nextChar: Char = 0
private var lexLength: Int = 0
private var position: Int = 0
private val lexems: util.List[String] = new util.ArrayList[String]
def start(line: String): Unit = {
initializeLexems()
sourceLine = line
position = 0
getChar()
getNextToken()
}
// A helper method to determine if the current character is a space.
private def isSpace(c: Char) = c == ' '
//Defined and intialized tokens
def initializeLexems(): Any = {
lexems.add("\\BEGIN")
lexems.add("\\END")
lexems.add("\\PARAB")
lexems.add("\\DEF[")
lexems.add("\\USE[")
lexems.add("\\PARAE")
lexems.add("\\TITLE[")
lexems.add("]")
lexems.add("[")
lexems.add("\\")
lexems.add("(")
lexems.add(")")
lexems.add("![")
lexems.add("=")
lexems.add("+")
lexems.add("#")
}
//val pattern = new regex("''").r
def getNextToken() ={
lexLength = 0
// Ignore spaces and add the first character to the token
getNonBlank()
addChar()
getChar()
// Continue gathering characters for the token
while ( {
(nextChar != '\n') && (nextChar != ' ')
}) {
addChar()
getChar()
}
// Convert the gathered character array token into a String
val newToken: String = new String(lexeme)
if (lookup(newToken.substring(0, lexLength)))
MyCompiler.setCurrentToken(newToken.substring(0,lexLength))
}
// A helper method to get the next non-blank character.
private def getNonBlank(): Unit = {
while ( {
isSpace(nextChar)
}) getChar()
}
/*
Method of function that adds the current character to the token
after checking to make sure that length of the token isn't too
long, a lexical error in this case.
*/
def addChar(){
if (lexLength <= 998) {
lexeme({
lexLength += 1; lexLength - 1
}) = nextChar
lexeme(lexLength) = 0
}
else
System.out.println("LEXICAL ERROR - The found lexeme is too long!")
if (!isSpace(nextChar))
while ( {
!isSpace(nextChar)
})
getChar()
lexLength = 0
getNonBlank()
addChar()
}
//Reading from the file its obtaining the tokens
def getChar() {
if (position < sourceLine.length)
nextChar = sourceLine.charAt ( {
position += 1;
position - 1
})
else nextChar = '\n'
def lookup(candidateToken: String): Boolean ={
if (!(lexems.contains(candidateToken))) {
System.out.println("LEXICAL ERROR - '" + candidateToken + "' is not recognized.")
return false
}
return true
}
}
else nextChar = '\n'<- this is where the condition goes after rendering the first character '\BEGIN' then just keeps outputting in the debug console as listed below.
This is what the debug console it outputting after '\BEGIN' is read through
Can anyone please let me know why that is? This happens after I keep stepping into it many times as well.
Here is the driver class that uses the lexical analyzer
import scala.io.Source
object MyCompiler {
//check the arguments
//check file extensions
//initialization
//get first token
//call start state
var currentToken : String = ""
def main(args: Array[String]): Unit = {
val filename = args(0)
//check if an input file provided
if(args.length == 0) {
//usage error
println("USAGE ERROR: Must provide an input file. ")
System.exit(0)
}
if(!checkFileExtension(args(0))) {
println("USAGE ERROR: Extension name is invalid make sure its .gtx ")
System.exit(0)
}
val Scanner = new MyLexicalAnalyzer
val Parser = new MySyntaxAnalyzer
//getCurrentToken(Scanner.getNextToken())
//Parser.gittex()
for (line <- Source.fromFile(filename).getLines()){
Scanner.start(line)
println()
}
//.......
//If it gets here, it is compiled
//post processing
}
//checks the file extension if valid and ends with .gtx
def checkFileExtension(filename : String) : Boolean = filename.endsWith(".gtx")
def getCurrentToken() : String = this.currentToken
def setCurrentToken(t : String ) : Unit = this.currentToken = t
}
The code is operating as it is supposed to. The first line contains only the string \BEGIN so the lexical analyser is treating the end of the first line as an '\n' as shown in this method:
def getChar() {
if (position < sourceLine.length)
nextChar = sourceLine.charAt ( {
position += 1;
position - 1
})
else nextChar = '\n'
However, the comment directly above that method does not describe what the method actually does. This could be a hint as to where your confusion lies. If the comment says it should read from the file, but it is not reading from the file, maybe that's what you've forgotten to implement.

Swift: Why the CustomStringConvertible description is run too many times in this case?

I was trying this code in Xcode Playground and noticed that the description getter method got called too many times.
The code is here: https://gist.github.com/T-Pham/4b72d17851162a32b2fc534f0618135d
First with both print lines, the code is run 3176 times.
Then with the first print commented out, the code is run 3164 times.
That means the first print would have to run the code 12 times.
However,
it is 148 times instead.
It is the playground that is messing with your head.
Playground is counting calls of its own for variables that have the CustomStringConvertibe protocol (probably to feed the information on the right side panel).
You can see this going on if you simply invoke mirror(tree) without printing at all.
If you count the actual number of invocations using your own counter, it will give a very different result:
var descCount = 0
extension Node: CustomStringConvertible {
var description: String
{
descCount += 1
return "id: \(id)\nleft: \(left)\nright: \(right)"
}
}
descCount = 0
print(tree)
descCount // 12
descCount = 0
print(mirror(tree))
descCount // 12
By the way, I had a little trouble understanding the mirror() function and I figured that a recursive one would probably be simpler to understand. How about adding a mirror() function to Node :
func mirror() -> Node
{
let result = Node()
result.id = id
result.left = right?.mirror()
result.right = left?.mirror()
return result
}
print(tree.mirror())
[EDIT] Here's a non-recursive mirror function (same logic as yours) with a somewhat clearer structure:
func mirror2(tree:Node) -> Node
{
// will return top of mirrored tree
let newTree = Node()
// node pair used for traversal and duplication
var original:Node! = tree
var mirrored:Node! = newTree
// traversal of tree structure left side first
// (uses mirrored tree to keep track of traversed nodes)
while original != nil
{
// carry node identifier (and contents eventually)
mirrored.id = original.id
// downwards, mirror left side first (if not already done)
if (original.left == nil) != (mirrored.right == nil)
{
original = original.left
mirrored.right = Node()
mirrored = mirrored.right
continue
}
// downwards, mirror right side second (if not already done)
if (original.right == nil) != (mirrored.left == nil)
{
original = original.right
mirrored.left = Node()
mirrored = mirrored.left
continue
}
// upwards from leaves and completed branches
original = original.parent
mirrored = mirrored.parent
}
return newTree
}
and some visual candy for tree descriptions:
extension Node: CustomStringConvertible
{
var indent:String
{ return " " + (parent?.indent ?? "") }
var description: String
{
return "\(id)\n"
+ ( left != nil ? "\(indent)L:\(left!)" : "" )
+ ( right != nil ? "\(indent)R:\(right!)" : "" )
}
}
resulting in an easier comparison of results:
print(tree)
// 0
// L:1
// L:3
// L:7
// R:8
// R:4
// L:9
// R:10
// R:2
// R:6
// L:13
// R:14
//
print(mirror2(tree))
// 0
// L:2
// L:6
// L:14
// R:13
// R:1
// L:4
// L:10
// R:9
// R:3
// L:8
// R:7

Changing an attribute in an object that belongs to RDD

I have the following code :
def generateStoriesnew(outputPath: String, groupedRDD:RDD[(String,Iterable[String])], isInChurnMode: Boolean, isInChurnPeriod: Boolean) {
val windowedRDD = groupedRDD.map(SOME CODE)
var windowedRDD2 = windowedRDD.filter(r => r != null).map(a=>a.churnPeriod(isInChurnPeriod,isInChurnMode))
val prettyStringRDD = windowedRDD2.map(r => {
r.toString
})
prettyStringRDD.saveAsTextFile(outputPath)
}
and here is the code for ChurnPriod function:
def churnPeriod( churnPeriod:Boolean, churnMode: Boolean): Unit = {
if (churnMode && rootEventType.equalsIgnoreCase("c")){
var churnCustStory: CustStoryN = null
var nonChurnCustStory: CustStoryN = null
var churnPeriodEventStory: mutable.MutableList[StoryEventN] = null
var NonChurnEventstory: mutable.MutableList[StoryEventN] = null
churnPeriodEventStory = new mutable.MutableList[StoryEventN]
NonChurnEventstory = new mutable.MutableList[StoryEventN]
var lastEventChurnPeriod = true
var currentEventStory = eventStory
var max = currentEventStory.length
println(max);
if (currentEventStory.size > 0) {
for (i <- 0 until currentEventStory.length) {
var currentEvent = currentEventStory(i)
if (currentEvent.timeSenseRootEvent < 90) {
churnPeriodEventStory.+=(currentEvent)
//lastEventChurnPeriod = true
}
else {
NonChurnEventstory.+=(currentEvent)
lastEventChurnPeriod = false
}
}
}
if (churnPeriod)
eventStory = churnPeriodEventStory
else
eventStory=null
}
}
but churn period function does not change eventstory which is a member of a custstory class. what am I missing here ?
class CustStoryN (val custId:String,
var rootEventType:String,
var rootEventTime:Long,
var eventStory:mutable.MutableList[StoryEventN])
my hypothesis is either:
1.map is not the right transformation for the function that I have
2.churnPeriod function never get called
3.I can not change eventstory which is a member of cust story class
Does anyone have any idea how I can troubleshoot this problem?
This would be trivial to determine by debugging. Just put a few breakpoints and you can see if the program stops inside the function, and what transformations do occur.
My guess would be that the problem is this line: if (currentEventStory.size > 0), thus, a list that starts at size 0 remains at size 0 forever. Another option is that churnPeriod is never true, thus, you compute a lot but never assign to the eventStory variable.
Your code does need a good cleanup ;-)

BSONDocument.getAs issue

I have 1 problem and 1 question when using BSONDocument getAs.
Whenever I try to access the value l in the format below by calling this:
docFound.getAs[Int]("v.1.0.2013.9.9.l")
it returns None. However if I do:
docFound.getAs[BSONDocument]("v")
it returtns a valid BSONDocument for the whole v section. What is wrong in my first call? Does reactivemongo support path traversal?
BSONDocument: {
v: {
1.0: {
2013: {
9: {
9: {
l: BSONInteger(0),
s: BSONInteger(8)
}
}
}
}
}
}
The second question is:
I find a document in DB with the following filter:
BSONDocument(
"_id" -> 0,
"v.1.0.2013.9.9.l" -> 1)
But it seems like instead of extracting just these values "_id" & "l" it extracts the whole document. When I do BSONDocument.pretty(foundDoc) I see the whole document, not just "l" value that I have requested. Please clarify if it is even worth specifying fields I am interested in if it always downloads the whole document.
Thanks.
It seems like according to the sources it is not supported in reactivemongo. So I have created a quick helper:
def getAsByPath[T](path: String, doc: BSONDocument)
(implicit reader: BSONReader[_ <: BSONValue, T]): Option[T] = {
val pathChunks = path.split('.')
var pathIndex = 0
var curDoc: Option[BSONDocument] = Some(doc)
var currentChunk = ""
while(pathIndex != pathChunks.size) {
currentChunk += pathChunks(pathIndex)
// If this is the last chunk it must be a value
// and if previous Doc is valid let's get it
if (pathIndex == pathChunks.size - 1 && curDoc != None)
return curDoc.get.getAs[T](currentChunk)
val tmpDoc = curDoc.get.getAs[BSONDocument](currentChunk)
if (tmpDoc != None) {
currentChunk = ""
curDoc = tmpDoc
} else {
// We need to support this as sometimes doc ID
// contain dots, for example "1.0"
currentChunk += "."
}
pathIndex += 1
}
None
}
However my second question is still valid. If someone knows please let me know.