Sunday 15 June 2014

html - Javascript Tic-tac-toe Game -


Everyone! Yesterday, I had started making a tick-tack game for a website that I wanted to make. After working about 10 hours of it, I just came up with around 150 lines code for the simple game. As always when I make a big chunk of code, something that I can not recognize, went wrong. My whole whole code did not work. Due to being a large part of the code, I paste my code in the jsFiddle project, the URL is shown below. My question is, can I use that type of array or something to reduce the number of statements and the number of securities? If anyone can help me make a faster and more efficient code, I would be very grateful. thank you in advanced!

jsfiddle.net/justinpchang/3L6tp/

Why not do something like this:

  / * This indicates the current game, declare it when starting a new game / var board = [[- -, '-', '-'], ['-' '-' '-'], ['-' - - '- -']]; Function dramaton (board, line, call) {return (board [row] [cola] == '-'); } Function hacks (board, line, call) {return (board [row] [cola] == 'x'); } // has the same function for   

, it eliminates some excess redundancy in your code. Apart from this, with HTML5 you can add custom attributes to your dividend elements, which will specify the row and column of the cell. For example, in the top-mid div there are properties of "data-line" and "data-call" and respectively there are properties of 0 and 1. I think the attribute names must start with "data-", but I'm not sure.

Oh and one more thing: Do not compare bullies to right or wrong, it is useless. Instead of doing: (some_bool! = False): (some_bool)

Since Boolean can only be true or false, similarly: (some_bool == incorrect) can be written here: ! (Some_bool)

Hope this will be helpful.

No comments:

Post a Comment