OXO

Learning to programのThe Basicsの最後でAlan Gauld先生に言われたようにOXOのプログラムを書いてみることにする。とりあえずDebian Referenceをさらっと読んで問題点は把握したのでそういう感じで進んでいくことが出来る。

まず、最初の壁にぶつかる。OXOってなんぞや?Wikipedia等で調べてもいまいちよくわからない。動画見てすぐわかった。http://www.youtube.com/watch?v=Pkiwn7u8i8s
あぁ、これか。

Wikipedia読んでもよくわからなかったけど、Wikipediaのほうに超適切くさいリンクが載っていたのでそこを参照しつつ進めていくことにする。
というか、まずこのプログラムどうやって動かすの状態。

The Edsac Simulator

Tutorial Guide※PDFがむちゃくちゃ今の自分に適切そうなのでこれを読んでいくことにする。ちょっと自分で考えて実行しろやおらああという感じでOXO作ってみてねと言われてたんだったら違うかもしれないけど、英語の勉強にもなるし、OXOってゲームはなんぞって感じなんで許してくだしぁ。

ていうか、起動の仕方はわかるけど、ゲームスタートの仕方すらわかんない状況だし。

In the late 1940s the EDSAC - and “electronic brains” in general - captured the public imagination and were widely reported in the press. Before you begin using the simulator you might like to read the newspaper headlines and extracts below; while not always accurate or temperate, they do capture the excitement of the period.
A Don Builds a Memory Short, dapper Dr. M.V. Wilkes, director of the Cambridge mathematical laboratory and ex-wartime radar backroom boy, is in charge of the calculator ... He told me yesterday: “The brain will carry out mathematical research. It may make sensational discoveries in engineering, astronomy, and atomic physics. It may even solve economic and philosophic problems too complicated for the human mind. There are millions of vital questions we wish to put to it.”

  • Daily Mail, October 1947

この文章熱すぎるぜ。
Electronic brains will carry out mathematical research. It may make sensational discoveries in engineering, astronomy, and atomic physics. It may even solve economic and philosophic problems too complicated for the human mind. There are millions of vital questions we wish to put to it.

This is how it works. First Mr Wilkes fed a strip of paper punched with holes into a “ticker-tape” machine. As the paper ticked through ... miniature television screens showed a row of green blobs ... then almost instantaneously a teleprinter nearby began to print rows of figures. That was all. There were no dramatic sparks, no dramatic flashes ...
There are not enough “brains” to go around at the moment, but a dozen would probably be sufficient for the whole country ... The future? The “brain” may one day come down to our level and help with our income-tax and book-keeping calculations. But this is speculation and there is no sign of it so far.

  • The Star, June 1949

1 GETTING STARTED

4, which cover programming and debugging, should be accessible to anyone acquainted with programming at the machine-code or assembler level. Finally, in Section 5 a number of programming problems are given, which range from elementary to quite difficult.

……なんだと?俺大丈夫かな。ついていけるかな。てか、アセンブリとか今やる必要は全くない感じなんだが。

読んでいって10ページぐらいでやっと気づいたけど、これはEDSACっていうコンピュータの説明みたいなもんで、それでどうやってOXOやるかっていう説明じゃ全くなかったwちょwww


さて、とりあえずoxo作ったわけだが。色々と考えた結果、EDSACのエミュレータのとおりにやれってことはAlan.Gauld先生も期待してないと思うので、適当に作ってみた。

# list variable - 3 colums 3raws - After user input - o, x = oxo OK?
# Loop block til game end
    # User input 1-9
    # Change Dict variable for that
    # If you can, this part is for computer brain's part.
    # If block - Check game end - If 3 colums 3raws ~
# Game end - exit game - if you can, print out which user win.

# Neccesary variables:3list(oxo main), User input value1-9,  

#
# Problem! Fill 9 Necessary to Draw.
#

import sys
row1 = [' ', ' ', ' ']
row2 = [' ', ' ', ' ']
row3 = [' ', ' ', ' ']
end = 0
o_count = 0 # For Draw Count

print ("Game Start")
while  end != 1:
    userInputO = int(raw_input("User_O Enter number1-9: "))
    if userInputO == 1:
        row1[0] = 'o'
    elif userInputO == 2:
        row1[1] = 'o'
    elif userInputO == 3:
        row1[2] = 'o'
    elif userInputO == 4:
        row2[0] = 'o'
    elif userInputO == 5:
        row2[1] = 'o'
    elif userInputO == 6:
        row2[2] = 'o'
    elif userInputO == 7:
        row3[0] = 'o'
    elif userInputO == 8:
        row3[1] = 'o'
    elif userInputO == 9:
        row3[2] = 'o'
    print ("%s%s%s" % (row1[0], row1[1], row1[2]))
    print ("%s%s%s" % (row2[0], row2[1], row2[2]))
    print ("%s%s%s" % (row3[0], row3[1], row3[2]))
    o_count += 1 # For Draw Count

    if ((row1[0] == 'o') and (row1[1] == 'o') and (row1[2] == 'o')) or ((row2[0] == 'o') and (row2[1] == 'o') and (row2[2] == 'o')) or ((row3[0] == 'o') and (row3[1] == 'o') and (row3[2] == 'o')) or ((row1[0] == 'o') and (row2[0] == 'o') and (row3[0] == 'o')) or ((row1[1] == 'o') and (row2[1] == 'o') and (row3[1] == 'o')) or ((row1[2] == 'o') and (row2[2] == 'o') and (row3[2] == 'o')) or ((row1[0] == 'o') and (row2[1] == 'o') and (row3[2] == 'o')) or ((row1[2] == 'o') and (row2[1] == 'o') and (row3[0] == 'o')):
        end = 1
        winner = 'User_O'
    elif o_count == 5: #For Draw
        end = 1
        winner = "No winner - Draw"
    else:
        userInputX = int(raw_input("User X Enter number1-9: "))
        if userInputX == 1:
            row1[0] = 'x'
        elif userInputX == 2:
            row1[1] = 'x'
        elif userInputX == 3:
            row1[2] = 'x'
        elif userInputX == 4:
            row2[0] = 'x'
        elif userInputX == 5:
            row2[1] = 'x'
        elif userInputX == 6:
            row2[2] = 'x'
        elif userInputX == 7:
            row3[0] = 'x'
        elif userInputX == 8:
            row3[1] = 'x'
        elif userInputX == 9:
            row3[2] = 'x'
        print ("%s%s%s" % (row1[0], row1[1], row1[2]))
        print ("%s%s%s" % (row2[0], row2[1], row2[2]))
        print ("%s%s%s" % (row3[0], row3[1], row3[2]))
        if ((row1[0] == 'x') and (row1[1] == 'x') and (row1[2] == 'x')) or ((row2[0] == 'x') and (row2[1] == 'x') and (row2[2] == 'x')) or ((row3[0] == 'x') and (row3[1] == 'x') and (row3[2] == 'x')) or ((row1[0] == 'x') and (row2[0] == 'x') and (row3[0] == 'x')) or ((row1[1] == 'x') and (row2[1] == 'x') and (row3[1] == 'x')) or ((row1[2] == 'x') and (row2[2] == 'x') and (row3[2] == 'x')) or ((row1[0] == 'x') and (row2[1] == 'x') and (row3[2] == 'x')) or ((row1[2] == 'x') and (row2[1] == 'x') and (row3[0] == 'x')):
            end = 1
            winner = 'User_X'
print ("Game end - Winner %s" % (winner)) 
sys.exit()

とりあえず汚いと自分でも反省はしているが、反省はしていない。動くからいいんだ。しかもセーフプルーフも全くない。
そんなものやろうとするとまたif文、if文とかになっちまう。
どうしたらいいのかどうしたら綺麗なソースコードを書けるのか。もっと学びたいと思う。
だが、これでoxoはできるのだ。対戦相手はコンピュータじゃないけど。
てか、コンピュータが対戦相手ってことはさすがに期待してないだろ?それだとアルゴリズムの領域だよね。

Wikipedia見てみると、oxoの戦略みたいなのも書いてあったからたぶんがんばれば出来ると思うんだけど。そういうアルゴリズムみたいなのはまた今度にさせてくだしぃ。

とりあえずこれで、またAlan Gauld先生のテキストに戻ってみようかなぁ。自分的にはこれで結構満足したし。
つか、俺すげえじゃん!みたいな感じですし。ソースコード明らかに糞汚いですし、関数とかファンクション?使ってませんけどね。

Alan Gauld先生のテキストに戻って、駄目そうだったら色々なものにまた手を広げてみるって感じでいいんじゃないかなー。
駄目そうじゃなくても色々手を広げていっても、もうこの段階からならいけるんじゃないかという、んなことないか。
でも勉強していきたいっす。いきたいsっす!