/// <summary> /// constructor - set up the input /// </summary> /// <param name="x"> x coordinate where to draw the input mask</param> /// <param name="y">y coordinate where to draw the input mask</param> /// <param name="radius"> how big the input mask should be</param> /// <param name="display"> display module</param> /// <param name="joystick"> joystick module</param> /// <param name="finishedHandler"> event handler which indicates if an input was finished</param> public ColorCodeInput(int x, int y, int radius, Gadgeteer.Modules.Module.DisplayModule display, Gadgeteer.Modules.GHIElectronics.Joystick joystick, EventHandler finishedHandler) { this.pos_x = x; this.pos_y = y; this.radius = radius; this.display = display; this.joystick = joystick; this.pos_code = 0; this.finishedHandler = finishedHandler; this.code = new ColorCode(); this.view = new ColorCodeView(pos_x,pos_y,radius,display,code); this.pos_joystick = new Joystick.Position(); }
/// <summary> /// displays the right code and win/loose message at the end of the game /// also controls the LED /// </summary> /// <param name="rightCode">the right (secret) code</param> /// <param name="gameWon">true if the game was won, false if the player lost</param> public void gameFinished(ColorCode rightCode, bool gameWon) { display.SimpleGraphics.DisplayText("The right code was:", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Blue, FINAL_CODE_X_POS, FINAL_CODE_Y_POS + TEXT_SIZE); ColorCodeView rightCodeView = new ColorCodeView(FINAL_CODE_X_POS+10, FINAL_CODE_Y_POS + CODE_SIZE*2 + TEXT_SIZE*2, CODE_SIZE, display, rightCode); rightCodeView.drawColorCode(); if (gameWon) { display.SimpleGraphics.DisplayText("You won!", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Green, FINAL_CODE_X_POS, FINAL_CODE_Y_POS); led.FadeRepeatedly(GT.Color.Green); } else { display.SimpleGraphics.DisplayText("You lost!", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Red, FINAL_CODE_X_POS, FINAL_CODE_Y_POS); led.FadeRepeatedly(GT.Color.Red); } }
/// <summary> /// Prints two blocks with text. If Joystick is pressed up, the One-Player-Mode is selected, if joystick is pressed down /// the Two-Player-Mode is selected /// </summary> /// <returns> ColorCode random generated or manually</returns> public void selectMode() { display.SimpleGraphics.Clear(); inputTimer.Tick += new GT.Timer.TickEventHandler(startSelection); inputTimer.Start(); joystick.JoystickPressed += joystick_JoystickPressed; cc = new ColorCode(); display.SimpleGraphics.DisplayText("Please pick a mode", font, Gadgeteer.Color.White, 20, 10); display.SimpleGraphics.DisplayRectangle(GT.Color.Cyan, 2, GT.Color.DarkGray, 18, 50, 80, 30); display.SimpleGraphics.DisplayTextInRectangle("One Player", 20, 50, 100, 50, Gadgeteer.Color.Yellow, font); display.SimpleGraphics.DisplayTextInRectangle("Two Player", 20, 80, 100, 50, Gadgeteer.Color.Yellow, font); }
// Dummy-Class for EventHandler, because not needed here // TODO: Remove after test public void unusedMethod(Object sender, EventArgs e) { cci.stop(); cc = cci.getCode(); inputFinished.Invoke(this,null); }
/// <summary> /// initializes and draws everything (new input field, last result code) for a new round /// </summary> /// <param name="lastResultCode">result code (correct colors, correct positions) of the last round</param> public void nextRound(ColorCode lastResultCode) { int round = masterMind.getCurrentRound(); ColorCodeView resultView = new ColorCodeView(RESULT_X_POS, (round + 2) * CODE_SIZE_SPACING * 2, CODE_SIZE, display, lastResultCode); resultView.drawColorCode(); createColorCodeInput(round); }
/// <summary> /// checks if ColorCodes match exactly, for game finish checks /// </summary> /// <param name="other">Colorsequence to check against</param> /// <returns>true if identical</returns> public bool isEqual(ColorCode other) { for (int i = 0; i < 4; i++) { if ( !colors[i].Equals( other.getColor(i) ) ) { return false; } } return true; }
/// <summary> /// Compares the given input code to the colors set in here. /// </summary> /// <param name="other">ColorCode that is compared</param> /// <returns>A color representation of the number of correctly colored and placed inputs. /// Cyan means correct input color at the right place, white that there is a correct color. /// </returns> public ColorCode compareCodes(ColorCode other) { ColorCode indicator = new ColorCode(); int index; int correctlyPlaced = 0; int rightColors = 0; GT.Color thisColor; GT.Color[] testcase = { colors[0], colors[1], colors[2], colors[3] }; //First find all correctly placed colors... for (index = 0; index < 4; index++) { thisColor = other.getColor(index); if (testcase[index].Equals(thisColor)) { correctlyPlaced++; //... and remove them, to not test against them again testcase[index] = correct; } } //Now find colors that are present in wrong places... for (index = 0; index < 4; index++) { if (testcase[index] != correct) //already found { thisColor = other.getColor(index); for (int i = 0; i < 4; i++) { if ( testcase[i].Equals(thisColor) ) { //found an incorrect placed color rightColors++; testcase[i] = semiRight; //remove and don't test for this index further... break; } } } } //fill the to be returned indicator... index = 0; for (int i = 0; i < correctlyPlaced; i++) { indicator.setColor(index, correct); index++; } for (int i = 0; i < rightColors; i++) { indicator.setColor(index, semiRight); index++; } return indicator; }
/// <summary> /// Initializes/resets the number of current round, clears display, starts the game /// </summary> /// <param name="secretCode"> Secret color code sequence for that game</param> void startGame(ColorCode secretCode) { display.SimpleGraphics.Clear(); currentRound = 0; secretColorCode = secretCode; /*for(int i=0;i<4;i++){ Debug.Print("color= " + secretColorCode.getColor(i).ToString()); }*/ inputFinished = new EventHandler(endOfRound); gameview = new GameView(this, display, joystick, led, inputFinished); }
/// <summary> /// Starts MasterMind game, and starts mode selection window to chse between Singleplayer and Multiplayer /// </summary> public void start() { ColorCode secretCode = new ColorCode(); ms = new ModeSelection(display, joystick, new EventHandler(modeFinished)); ms.selectMode(); }
/// <summary> /// changes the color of the given color code at a certain position /// </summary> /// <param name="d"> the direction (UP or DOWN)</param> /// <param name="code"> the color code</param> /// <param name="pos"> position of the color to be changed in the code </param> private void changeColor(direction d, ColorCode code, int pos) { if (d == direction.UP) { code.setColor(pos, ColorCode.getNextColor(code.getColor(pos))); } else { code.setColor(pos, ColorCode.getPrevColor(code.getColor(pos))); } }