Advertisement

tic tac toe

Started by October 21, 2019 04:47 PM
61 comments, last by 8Observer8 5 years, 1 month ago

If it is difficult you can use simple "if/else" construction. Write a code as simple as you can. On the next step you can refactor it. I will show you a very simple example using JavaScript and <canvas> element that you can run in JSFiddle: https://jsfiddle.net/8Observer8/jhzef4dx/18/

I created a canvas elemen with the size 300x300


<canvas id="myCanvas" width="300" height="300"></canvas>

I wrote a handler for mouse click. I show a message when conditions is true:


var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

// Draw a circle
ctx.beginPath();
ctx.arc(50, 50, 30, 0, 2 * Math.PI);
ctx.stroke();

canvas.onclick = function(event) {
  var x = event.offsetX;
  var y = event.offsetY;
  if (0 < x && x < 100 && 0 < y && y < 100)
  {
  	alert("it is left top corner " + x + " " + y);
  }
  else if (200 <= x && x < 300 && 200 < y && y < 300)
  {
  	alert("it is right bottom corner " + x + " " + y);
  }
}

 

1 hour ago, phil67rpg said:

wow there is a lot of conversation about if statements. happy halloween

The term for this is bikeshedding.

Advertisement
20 minutes ago, JTippetts said:

The term for this is bikeshedding.

Just to provide an alternate perspective, some of the 'bike shed' issues under discussion here are fairly fundamental and can make a difference as a project scales up, so it may not necessarily be a waste of time to point them out. I'd agree though that there are larger issues at play here that such tangents don't (and probably can't) address.

2 hours ago, Zakwayda said:

there are larger issues at play here

You're talking about the K&R style braces, right? Or is it the tab spacing?

Phil is playing tic tac toe white the rest of you are playing code golf.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

3 hours ago, fleabay said:

You're talking about the K&R style braces, right? Or is it the tab spacing?

I assume you're just being facetious, but just in case it's not clear, by 'larger issues' I mean non-bikeshed, non-trivial issues that are unlikely to be resolved in this thread. (For the record, I just jumped in to point out that the point-in-box test could and arguably should be refactored, and that it's not necessary to loop over the cells to find the cell that was hit. I have no particular investment beyond that.)

Zakwayda's tip to avoid a double for loop (and avoid the original issue of 50 if statements) is fantastic advice for this level of assignment. Comparing Tic-Tac-Toe to a nuclear powerplant is absurd.

Advertisement
22 minutes ago, Euthyphro said:

Comparing Tic-Tac-Toe to a nuclear powerplant is absurd.

He was comparing an if statement to a bike shed. Is that better for you?

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

He needs comments, a license agreement and a github source control page for the project. 
Single file with more than 100 lines of code is already too big of a project. Split it into 7 parts, and make it OOP. Don't forget to use delegates and interfaces for extensibility. 
Also move the engine to a separate dll. 

I am still working on my AI for my game.

4 hours ago, phil67rpg said:

I am still working on my AI for my game.

This isn't twitter.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

This topic is closed to new replies.

Advertisement