{"id":330,"date":"2012-11-06T06:50:09","date_gmt":"2012-11-06T06:50:09","guid":{"rendered":"http:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/?p=330"},"modified":"2012-11-06T07:04:11","modified_gmt":"2012-11-06T07:04:11","slug":"interactive-game-in-processing","status":"publish","type":"post","link":"https:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/2012\/11\/06\/interactive-game-in-processing\/","title":{"rendered":"Interactive Game in processing"},"content":{"rendered":"<p>For our second project, creating an interactive image in processing using various programming concepts, I created a variation of the kid&#8217;s game Mastermind. This involved input, for loops, variables, random numbers, functions, and conditionals.<\/p>\n<p>Instructions:<br \/>\n&#8211; In this version there are 6 colors: white, green, red, teal, yellow, and magenta.<br \/>\n&#8211; Click on a box in the current row to cycle through the colors.<br \/>\n&#8211; The answer will be some set of 3 colors and a given color can repeat (example: teal, teal white or red, yellow, green). &#8211; When you are satisfied with your guess press the guess button.<br \/>\n-The two circles above each square may be filled blue based on your results. No circles colored above the square means that the color does not appear in the correct sequence, the first circle colored means that the color is in the sequence but not in that location (this does not indicate how many of that color are in the sequence. If the answer is red, yellow, yellow and you guess red,red,green  than the first box will have two filled and the second will have one filled even though you already guessed the only red in the sequence).<br \/>\n&#8211; If your guess is correct a smiley face will show up at the bottom. If it is not correct a new row will show up for you to guess with unless you have guessed 5 times.<br \/>\n&#8211; Previous rows stay there for you to see what you have already guessed.<br \/>\n&#8211; If you have guessed 5 times without winning a frowning face will show up at the bottom indicating that you lost the game.<br \/>\n&#8211; After one game is finished (win or loss) you can click anywhere to start a new game.<br \/>\n&#8211; The answer is randomly generated each time.<\/p>\n<p><script type=\"application\/processing\">\r\nint tries = 0;\r\nint[] answers = new int[3];\r\nint[] filled = new int[3];\r\nboolean won = false;\r\nboolean lost = false;\r\n\r\nvoid setup(){\r\n  \/\/create the guess button and the first row.\r\n  \/\/Set up the state of the buttons in the row, and set up the first\r\n  \/\/answer.\r\n  size(150,400);\r\n  background(0);\r\n  fill(100);\r\n  rect(10, 10,80,30);\r\n  fill(0);\r\n  text(\"Guess\",30,30);\r\n  fillAnswers();\r\n  tries = 0;\r\n  won = false;\r\n  lost = false;\r\n  row();\r\n}\r\n\r\nvoid row(){\r\n  \/\/reset filled\r\n  filled[0] = 0;\r\n  filled[1] = 0;\r\n  filled[2] = 0;\r\n  fill(255);\r\n  stroke(255);\r\n  int circleY = 55 + (tries * 50);\r\n  int circleX = 15;\r\n  for(int i = 0; i < 6; i++){\r\n    ellipse(circleX,circleY,5,5);\r\n    circleX = circleX + 20;\r\n  }\r\n  int rectX = 15;\r\n  int rectY = circleY + 15;\r\n  \r\n  for(int i = 0; i < 3; i++){\r\n    rect(rectX, rectY, 30, 20);\r\n    rectX = rectX + 40;\r\n  }\r\n}\r\n\r\nvoid draw(){\r\n  \/\/mouse pressed won't work if there is not a draw function\r\n}\r\n\r\nvoid mousePressed(){\r\n  if(won || lost)\r\n  {\r\n    setup();\r\n    return;\r\n  }\r\n  if((70 + (tries * 50)) <= mouseY && mouseY <= (90 + (tries * 50))){\r\n    int rectY = 70 + (tries * 50);\r\n    if(mouseX > 10 && mouseX < 40){\r\n      changeFill(0);\r\n      rect(15,rectY,30,20);\r\n    }\r\n    else if(mouseX > 50 && mouseX < 80){\r\n      changeFill(1);\r\n      rect(55,rectY,30,20);\r\n    }\r\n    else if(mouseX > 90 && mouseX < 120){\r\n      changeFill(2);\r\n      rect(95,rectY,30,20);\r\n    }\r\n  }\r\n  if(mouseY > 10 && mouseY < 40 && mouseX > 10 && mouseX < 90) {\r\n    if(tries < 4){\r\n      if(!(results() == 3)){\r\n         tries++;\r\n         row();\r\n      }\r\n      else{\r\n        gameEnd(1);\r\n      }\r\n    }\r\n    else{\r\n      if(results() == 3)\r\n        gameEnd(1);\r\n      else\r\n        gameEnd(0);\r\n    }\r\n  }\r\n}\r\n\r\nvoid changeFill(int button) {\r\n  \/\/ check which color the current button is on. Set fill to this color\r\n  \/\/ and update that part of the array to the next color\r\n  \/\/The colors go: white, green, red, teal, yellow, purple\r\n  int fillCol = filled[button];\r\n  if(fillCol == 0){\r\n    filled[button] = 1;\r\n    fill(0,255,0);\r\n  }\r\n  else if(fillCol == 1) {\r\n    filled[button] = 2;\r\n    fill(255,0,0);\r\n  }\r\n  else if(fillCol == 2) {\r\n    filled[button] = 3;\r\n    fill(0,255,255);\r\n  }\r\n  else if(fillCol == 3) {\r\n    filled[button] = 4;\r\n    fill(255,255,0);\r\n  }\r\n  else if(fillCol == 4){\r\n    filled[button] = 5;\r\n    fill(255,0,255);\r\n  }\r\n  else {\r\n    filled[button] = 0;\r\n    fill(255);\r\n  }\r\n}\r\n\r\nint results(){\r\n  int circleY = 55 + (tries * 50);\r\n  int circleX = 15;\r\n  int correct = 0;\r\n  \/\/ both black means wrong color wrong place, one lit blue means right color\r\n  \/\/ wrong place, both blue means right color right place.\r\n  \/\/ return the number of buttons the player got correct.\r\n  for(int i = 0; i < 3; i++)\r\n  {\r\n    fill(0,0,255);\r\n     for(int j = 0; j < 3; j++){\r\n       if(filled[j] == answers[i]){\r\n          ellipse(circleX,circleY,5,5);\r\n          circleX += 20;\r\n          if(j == i){\r\n            ellipse(circleX,circleY,5,5);\r\n            correct++;\r\n          }\r\n          circleX +=20;\r\n       }\r\n       else{\r\n         circleX += 40;\r\n       }\r\n    }\r\n    circleX = 15;\r\n  }\r\n  return correct;\r\n}\r\n\r\nvoid gameEnd(int condition){\r\n  \/\/show a happy or sad face to indicate win or lose condition\r\n  fill(255,255,0);\r\n  ellipse(70,340,60,60);\r\n  fill(0,0,255);\r\n  ellipse(55,335,10,10);\r\n  ellipse(85,335,10,10);\r\n  stroke(0);\r\n  if(condition == 1){\r\n    line(70,360,75,360);\r\n    line(75,360,80,355);\r\n    won = true;\r\n  }\r\n  else{\r\n    line(70,350,75,350);\r\n    line(75,350,80,355);\r\n    lost = true;\r\n  }\r\n}\r\n\r\nvoid fillAnswers(){\r\n  for(int i = 0; i < 3; i++){\r\n    answers[i] = int(random(6));\r\n  }\r\n}\r\n<\/script><br \/>\nEDIT, PLEASE NOTE: For some reason right now the game isn&#8217;t working here even though it works when I run it in processing. I am going to look into this<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For our second project, creating an interactive image in processing using various programming concepts, I created a variation of the kid&#8217;s game Mastermind. This involved input, for loops, variables, random numbers, functions, and conditionals. Instructions: &#8211; In this version there &hellip; <a href=\"https:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/2012\/11\/06\/interactive-game-in-processing\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/wp-json\/wp\/v2\/posts\/330"}],"collection":[{"href":"https:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/wp-json\/wp\/v2\/comments?post=330"}],"version-history":[{"count":2,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/wp-json\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":332,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/wp-json\/wp\/v2\/posts\/330\/revisions\/332"}],"wp:attachment":[{"href":"https:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/wp-json\/wp\/v2\/media?parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/wp-json\/wp\/v2\/categories?post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/IMGD3x00_B12\/wp-json\/wp\/v2\/tags?post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}