Jump to content

TicTacToe v002 with improved AI, again i hope it is helpful, the code could be optimized by using...


G+_Scott B
 Share

Recommended Posts

Single test function version: http://pastebin.com/L5VqxD5h

After seeing Lee's brilliant "wins" list I noticed that both test functions are looping over the potential win paths and it would be a fun exercise to combine them into a single function for even more consolidation. I don't think this way is any better because it probably has more overhead. I'm simply posting it as an example of another way of doing it and also to show Python's powerful "list comprehensions" and "generators" because they probably won't get covered in the show.

Link to comment
Share on other sites

i used the two loop using the range() command for the tests but decide to post the code using if else. i know its not the best practices   but it is easier to read for a beginner than a nested command, i was quite impressed with both of the examples and i am sure they will help people understanding lists.

Link to comment
Share on other sites

noticed an error in your program

random.randrange(1,9)

this will only pick numbers 1,2,3,4,5,6,7,8

meaning the computer will never randomly pick 9

it can still mark 9 if its trying to block or win and because player 1 always goes first you cant make 9 the only spot left for computer to pick randomly but if you do change it to alternate turns it can cause an endless 'print "the move was invalid or space has been taken" ' loop

The only problem with it in its current implementation is that you can use it for a unfair advantage, but since its a computer that just picks randomly its not much of an advantage anyway.  just thought you should know.

Link to comment
Share on other sites

  • 2 weeks later...

Darryl Medley question about your implementation: I notice you use "list comprehension" and "generators" - I've just read up on what they are and they seem very powerful. Am I correct in saying that they both do essentially the same thing but the list comprehension is for when you want to keep the list, but the generator is for when you want to use it only to feed another function/statement?

Link to comment
Share on other sites

There are no generators in this code. Your impression of their use is pretty good, though. Comprehensions are a handy syntax for filling a list with a short piece of code. Generators are for producing sequences on-the fly that may not even fit in memory, or whose values may not be knowable until they are produced.

Link to comment
Share on other sites

I found that "next" code online. I understood that the expressing inside of the next function, ((idx for idx, mrk in enumerate(boardMarks) if mrk != mark1)), is a "generator expression" but if it's not then I'm sorry for the misnomer. Lionel, The way I understand it is that list comprehensions are shorthand for quickly processing a list while generators spit out one value at a time like when next() is called but I'm new to this stuff as well so my understanding is limited.

Link to comment
Share on other sites

Lee Crocker I was under the impression that a double parentheses meant a generator and the braces meant comprehension:

 

return w[next((idx for idx, mrk in enumerate(boardMarks) if mrk != mark1))]

 

I've been playing around with the comprehensions on some of my old code and find that it comes in very handy and reduces a lot of code in some cases. But I'm sure I'm probably overusing it now :)

Link to comment
Share on other sites

 Share

×
×
  • Create New...