# Tictactoe and choice

**URL:** <https://www.ruby-forum.com/t/tictactoe-and-choice/157158>\
**Category:** Ruby\
**Created:** [January 7, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/tictactoe-and-choice/157158 "2009-01-07T23:40:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [January 7, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/tictactoe-and-choice/157158/1 "2009-01-07T23:40:55Z")

</div>

I was trying to code the classic tictactoe in ruby.  
Now it only plays itself doing random moves until someone wins, and  
there  
are still some bugs.

But the thing that really I don’t understand is another,

With this line every time I pick the next move:

x, y = (0…(board.length)).to\_a.choice, (0…  
(board.length)).to\_a.choice

But I get always the same game!!:

step 0 we have:  
[[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]

step 1 we have:  
[[nil, nil, “O”], [nil, nil, nil], [nil, nil, “X”]]

step 2 we have:  
[[nil, “O”, “O”], [“X”, nil, nil], [nil, nil, “X”]]  
sorry 2, 2 already set  
sorry 0, 2 already set

step 3 we have:  
[[nil, “O”, “X”], [“X”, nil, nil], [“O”, “X”, “X”]]  
player X has won  
[[nil, “O”, “X”], [“X”, nil, “X”], [“O”, “X”, “X”]]

It looks like there’s nothing really random here, am I missing  
something maybe??  
The full source code

[http://pastie.textmate.org/private/ivw3qswnf6rbppyd78m0qq](http://pastie.textmate.org/private/ivw3qswnf6rbppyd78m0qq)

Thanks a lot for any hint

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [January 8, 2009, 3:55am UTC](https://www.ruby-forum.com/t/tictactoe-and-choice/157158/2 "2009-01-08T03:55:36Z")

</div>

Hello andrea,

> (board.length)).to\_a.choice
> 
> But I get always the same game!!:
> 
> [snip]

You should add a call to srand before the call to random\_play. It  
will seed the random number generator so that you don’t get the  
same sequence of random numbers every time you run your program.

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [January 8, 2009, 9:50am UTC](https://www.ruby-forum.com/t/tictactoe-and-choice/157158/3 "2009-01-08T09:50:20Z")

</div>

Thanks a lot now it’s working pretty nicely

[http://pastie.textmate.org/private/0qfnvswccjfqhbe73ql7q](http://pastie.textmate.org/private/0qfnvswccjfqhbe73ql7q)

There are still a few things that I don’t like though.  
For example here  
def reset\_board(len = 3)  
board = []  
free = []  
(0…(len)).each { board \<\< [nil] \* len }

# setting all the free cells

for i in (0…(len))  
for j in (0…(len))  
free \<\< [i, j]  
end  
end  
[board, free]  
end

I generate the empty board and the free places, can I generate free  
with  
only one block?

Same thing in the check procedure, I’m first trying to call chkline  
for every line,  
creating the array of truth values and then calling truth.any?  
It’s a little be boring, I would like to do

and [check x | x ← gen\_lines board]

(as I would do in Haskell more or less)

def check(board)  
chk = lambda { |vals| vals.any? and vals.uniq.length == 1 }  
truth = []  
chk\_line = lambda { |b| b.each { |line| truth \<\< (chk.call line) }}  
chk\_line.call gen\_lines board  
chk\_line.call gen\_lines board.transpose

# truth is automatically updated in the two calls

truth.any?  
end

def gen\_lines(board)  
diag = []  
to\_check = []  
(0…(board.length)).each { |i| diag \<\< board[i][i]; to\_check \<\<  
board[i] }  
to\_check \<\< diag  
to\_check  
end

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [January 8, 2009, 1:30pm UTC](https://www.ruby-forum.com/t/tictactoe-and-choice/157158/4 "2009-01-08T13:30:36Z")

</div>

On 8 Gen, 10:09, Jesús Gabriel y Galán [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> # end
> 
> # end
> 
> # Not in a single loop but I’d do:
> 
> 0.upto(len-1) {|i| 0.upto(len-1) {|j| free \<\< [i,j]}}
> 
> > [board, free]  
> > end
> 
> Jesus.

Thanks a lot, with classes is much much better and cleaner  
[http://pastie.textmate.org/private/hompwzt9vu5oz7dkwryrg](http://pastie.textmate.org/private/hompwzt9vu5oz7dkwryrg)

Now I have to implement the decision engine and that’s it, I could be  
satisfied.

I wanted to try to use callcc for the decision tree (along with a  
normal backtrack stack),  
could it be a good idea?

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [January 8, 2009, 10:09am UTC](https://www.ruby-forum.com/t/tictactoe-and-choice/157158/5 "2009-01-08T10:09:49Z")

</div>

On Thu, Jan 8, 2009 at 9:49 AM, andrea [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There are still a few things that I don’t like though.  
> For example here  
> def reset\_board(len = 3)

# board = []

# (0…(len)).each { board \<\< [nil] \* len }

board = Array.new(len) {[nil]\*len}

free = []

# setting all the free cells

# for i in (0…(len))

# for j in (0…(len))

# free \<\< [i, j]

# end

# end

# Not in a single loop but I’d do:

0.upto(len-1) {|i| 0.upto(len-1) {|j| free \<\< [i,j]}}

> [board, free]  
> end

Jesus.
