Other Websites

  • SeaCloud9 Interactive
  • SeaCloud9 Commercial Development
  • i-os
  • stArcade9

Brendon Smith Social Networks

  • On Linkedin
  • Bookmarks
  • On Twitter
  • On Facebook
Open ↓ Close ↑
  • Home
  • Subscribe
Browse: Home / Game Creation, i-create, JavaScript / CodeBreaker a Game of Deduction
i-create | therefore-i am
i-create | therefore i-am | a blog about opensource technology and rich internet applications
 

CodeBreaker a Game of Deduction

By Brendon Smith on April 4, 2010

 

Let me first start of by simply telling you I have been reading Flash Game University by Gary Rosenzweig.  This is an excellent resource for anyone who wants to teach themselves how to create games.  The examples in the book are relatively simple but of course the logic that goes into creating them is not.  While reading and studying the examples I have decide to convert some of the examples over to JavaScript not because I think JavaScript is better etc. etc. Personally I hate reading articles where people flame Adobe and say flash is dead etc.. Most of the time the people who write these articles can’t do both I can so I like both! Simply because I thought it would help me along in the thinking process.  Some people play Bridge, Chess, Sudoku, or Crossword Puzzles to stay sharp I write code and study code to stay sharp.  So in his book he has an example that is similar to the MasterMind board game.  I decided to port over this game to JavaScript and have done this you can find the example by clicking here. If you like this example please purchase his book it is an excellent book and this code was generated by studying his example so please pay him tribute and purchase the book like I have.  A little background about the game if it says you have a correct spot and the correct color. If it says you have the correct color you have the correct color but it is in the incorrect spot.

 		// constants
        const numPegs = 5;
        const numColors = 5;
        const maxTries = 10;
        const horizOffset = 30;
        const vertOffset = 60;
        const pegSpacing = 30;
        const rowSpacing = 30;
 
        // game play variables
        var solution = new Array();
		var solutionButtonArray;
        var turnNum;
 
        // references to display objects
		var buttonImages = new Array("img/cbBlack.png","img/cbGreen.png","img/cbYellow.png","img/cbPurple.png","img/cbRed.png", "img/cbBlue.png" );
        var currentRow = new Array();
        var currentText;
		var currentButton;
	    var mainIds = 0;
		var myButton;
		function startGame() {
			for(var i =0;i<numPegs;i++) {
				// random, from 0 to 4
				var r = Math.floor(Math.random()*numColors) + 1;
				solution.push(r);
			}
			turnNum = 0;
			createPegRow();
		}
 
	   function createPegRow(start) {
		   solutionButtonArray = new Array();
		   if(start == undefined){
		   $('#cbMainGame').html("");
		   }
		   var cbButton1 = '<div class="divCButton" id="div';
		   var cbButton2 = '" onclick="clickButton(this)"><img id="img';
		   var cbButton3 = '" alt="0" src="img/cbBlack.png" /></div>';
		   var mainWrapper1 = '<div class="mainWrapper">';
		   var mainWrapper2 = '</div>';
		   $('#cbMainGame').append(mainWrapper1);
		   var btnFinished = '<div class="imgButton" id="divButtonFinished';
		   var btnFinished2= '"><center><div class="mainButton" id="divButtonDone';
		   var btnFinished3= '" onclick="executeGameQuery(this)"><span id="spnMainButton">Done</span></div></center></div>';
	   	   currentRow = new Array();
		   for(i=0;i<numPegs;i++) {
				$('#cbMainGame').append(cbButton1+mainIds+cbButton2+mainIds+cbButton3);
				solutionButtonArray.push("img"+mainIds);
				mainIds++;
			}
			$('#cbMainGame').append(btnFinished+mainIds+btnFinished2+mainIds+btnFinished3+mainWrapper2);
	   }
 
	   function clickButton(clicked){
		   var myButton = clicked;
		   var img = $(clicked).find('img');
		   var imageToChange = img[0].alt.toString();
		   if(imageToChange == 6){
			imageToChange = 0;   
		   }
		   //console.log(imageToChange);
		   $("#"+img[0].id).attr("src",buttonImages[parseInt(imageToChange) + 1]);
		   $("#"+img[0].id).attr("alt", parseInt(imageToChange) + 1);
	   }
 
	   function executeGameQuery(mainButton){
		   var buttonClicked = mainButton;
		   var changeThis = buttonClicked.id;
		   var currentColorList = new Array();
		   var colorList = new Array(0,0,0,0,0);
		   var soltionList = new Array(0,0,0,0,0);
		   changeThis = $("#"+changeThis).parent().parent();
		   //console.log(changeThis);
		   var numberCorrect = 0;
		   var numberColorsCorrect = 0;
		   turnNum++;
		   for(i=0; i<numPegs; i++){
			   currentColorList.push($("#"+solutionButtonArray[i]).attr("alt"));
			   $("#"+solutionButtonArray[i]).parent().removeAttr("onclick");
		   }
		   for(i=0; i<numPegs; i++){
			  if(currentColorList[i] == solution[i] ){
				  numberCorrect++;
			  }else{
				  soltionList[solution[i]]++;
				  colorList[parseInt(currentColorList[i])]++;
			  }
		   }
		   for(i=0; i<numColors; i++){
			   var test = Math.min(soltionList[i],colorList[i]);
				numberColorsCorrect = numberColorsCorrect + Math.min(soltionList[i],colorList[i]);
			}
		   $("#"+buttonClicked.id).remove();
		   if(numberCorrect == numPegs){
		   $(changeThis).html('You Got It!<br/><div class="imgButton"><center><div onclick="startGame()" id="divButtonDone5" class="mainButton"><span id="spnMainButton">Play Again</span></div></center></div>');
		   }else if(turnNum == maxTries){
			   $(changeThis).html('I regret to infrom you just lost next time do it quicker! <br/><div class="imgButton"><center><div onclick="startGame()" id="divButtonDone5" class="mainButton"><span id="spnMainButton">Play Again</span></div></center></div>');
 
		   }
		   else{
		   $(changeThis).html("Correct Spot: "+numberCorrect+", Correct Color: "+numberColorsCorrect);
		   var start = true;
		   createPegRow(start);
		   }
	   }
$(document).ready(function() { 
$('#cbMainGame').append(startGameHTML);						   
});

Launch Game / Download Code

I have also been spending some time playing around with Alchemy last post I displayed some examples of RayCasters the old style 3d of Games like Wolfenstien and Doom.  In short most of the raycasters out their follow this example this example is a c++ example and Adobe has a project in their labs called Alchemy.  Alchemy allows you to port existing C/C++ to ActionScript.  Which is really cool but also extremely difficult to do:)  Right now I need to rewrite a chunk of it that uses SDL lib an old library that was used to create most video games years ago.  I searched to see if someone was doing something similar and sure enough someone is Bruce Jawn.  He informed me that instead of using SDL I need to pass it to the screen buffer as a byte array.  So that is currently what I am trying to do he also mentioned posting his Make file under Google code in the near future.  Hopefully he does I have also noticed haXe has an SDL wrapper.  This might also be useful to study RayFaster2 this is a RayCaster built with haXe that is super fast.  Their are numerous examples of RayCasters written in ActionScript but I am looking for speed.  I might think about also just writing one from scratch and implementing a JavaScript version of it as well.  I could have spent this weekend working on my new portfolio but unfortunately I didn’t do that:(  I will have to extend it deadline beyond the 4 days time limit once again:(

  • Share/Bookmark

Related posts:

  1. Indie Game Developer
  2. WebGL TweetTank Built with C3DL Part 2
  3. Video Game Industry
  4. HTML5 jQuery jqPanoramic Plugin Alpha
  5. WebGL TweetTank Built with C3DL Part 1

Posted in Game Creation, i-create, JavaScript | Tagged Experiment, gameCreation, Games, JavaScript, JQuery, RayCasting

Brendon Smith

GUI Development, Action Script, Java Script, .NET, AJAX, Java, PHP, CakePHP, Mashup Development, Flash, Silverlight, C#, XML, SQL, Apache, IIS, Photoshop, Fireworks,etc.. Oh, and Biking and Camping

blog comments powered by Disqus
« Previous Next »
 
 

3d ActionScript Adobe Air Android Apollo Apple art as3 Away3D C# CakePHP CSS Experiment Flash Flash Develop Flex Games Generative Design Google HTML5 hype JavaScript Joshua Davis JQuery JSON Life Linux MashUp OpenSource Open Source PaperVision3D PC History Processing quick tip RIA Silverlight Technology/Internet travel twitter Web webGL Webware XML Yahoo Pipes

WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.

  • Monthly
  • Yearly
  • Links
  • August 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • agit8
  • Away3D
  • Ben Nadel
  • Bit-101
  • Bruce Jawn
  • Causecast
  • D.I.Y.
  • Dr Woohoo
  • draw.logic
  • Flight404
  • Flong
  • generatorX
  • gSkinner
  • haXe
  • Jonathan Snook
  • Joshua Davis
  • Jot
  • Kirupa
  • LifeHacker
  • Make
  • Minor White
  • Mr Doob
  • NihiLogic
  • NurseOnTheRun
  • octane42
  • OpenFrameWorks
  • Processing
  • PV3D
  • Senocular
  • Sephiroth
  • ShineDraw
  • Stroep
  • SWX
  • Tech News
  • Toxi
  • UnitZeroOne
  • World We Live In
  • ZeusLabs

Photos

0510225_0510225-R1-052-24A.jpg

Recent Comments

  • seacloud9 on Tweet Tank in Away3D and Hype pt. 2
  • Brendon Smith on Tweet Tank in Away3D and Hype pt. 2

RSS LifeStream

  • Elysium
  • Giles Bowkett: Archaeopteryx: A Ruby MIDI Generator
  • Grant Nestor » Blog Archive » Generative music, huh?

Copyright © 2010 i-create | therefore-i am.