diff --git a/bot.py b/bot.py index 2b45d4f..03a2a9d 100644 --- a/bot.py +++ b/bot.py @@ -7,6 +7,7 @@ def get_sudoku_moves(): PLAYER_2 = "O" EMPTY = "_" Move = namedtuple("Move", ["ps", "n"]) + Draw = namedtuple("Move", ["ps", "t"]) Position = namedtuple("Position", ["t", "i"]) def all_rows(field): @@ -69,12 +70,23 @@ def get_sudoku_moves(): equities.append(equity) return min(equities) * -1 - def get_moves(field, turn, moves, moves_acc, bots_turn): + moves_acc = [] + draws_acc = [] + wins2_acc = [] - if player_won(field, PLAYER_1) or player_won(field, PLAYER_2): + def get_moves(field, turn, moves, bots_turn): + + if player_won(field, PLAYER_1): + raise Exception("If we got here our bot screwed up.") + + if player_won(field, PLAYER_2): + moves = Draw(moves.ps, turn) + wins2_acc.append(moves) return if game_over(field): + moves = Draw(moves.ps, turn) + draws_acc.append(moves) return if not bots_turn: @@ -83,7 +95,7 @@ def get_sudoku_moves(): new_field[move] = PLAYER_1 new_field = tuple(new_field) new_moves = Move(moves.ps + [Position(turn, move)], None) - get_moves(new_field, turn + 1, new_moves, moves_acc, True) + get_moves(new_field, turn + 1, new_moves, True) else: equities = [] for move in possible_moves(field): @@ -98,8 +110,8 @@ def get_sudoku_moves(): new_field = tuple(new_field) bot_move = Move(moves.ps, Position(turn, move)) moves_acc.append(bot_move) - get_moves(new_field, turn + 1, moves, moves_acc, False) - return moves_acc + get_moves(new_field, turn + 1, moves, False) EMPTY_FIELD = tuple([EMPTY for _ in range(9)]) - return get_moves(EMPTY_FIELD, 0, Move([], None), [], False) + get_moves(EMPTY_FIELD, 0, Move([], None), False) + return (moves_acc, draws_acc, wins2_acc) diff --git a/readme.md b/readme.md index c90d4a0..6c09ac4 100644 --- a/readme.md +++ b/readme.md @@ -4,4 +4,6 @@ https://eddyerburgh.me/make-responsive-tic-tac-toe-board https://css-tricks.com/game-life/ -https://codepen.io/ziga-miklic/post/pure-css-tic-tac-toe \ No newline at end of file +https://codepen.io/ziga-miklic/post/pure-css-tic-tac-toe + +https://jsfiddle.net/stackmanoz/r6E9p/ \ No newline at end of file diff --git a/template.tictactoe.css b/template.tictactoe.css index 9154116..3d73bd0 100644 --- a/template.tictactoe.css +++ b/template.tictactoe.css @@ -4,28 +4,107 @@ body { } .tic-tac-toe { - height: 450px; - width: 450px; + font-family: 'Open Sans', sans-serif; + height: 440px; + width: 440px; margin: 50px auto 30px auto; position: relative; - background-color: #E6E6FA; +} + +.tic-tac-toe .grid { + background-color: grey; + position: absolute; + display: block; + border-radius: 3px; +} + +.tic-tac-toe .horizontal-bar-1 { + top: 140px; + bottom: 290px; + height: 10px; + width: 440px +} + +.tic-tac-toe .horizontal-bar-2 { + top: 290px; + bottom: 140px; + height: 10px; + width: 440px +} + +.tic-tac-toe .vertical-bar-1 { + left: 140px; + right: 290px; + height: 440px; + width: 10px; +} + +.tic-tac-toe .vertical-bar-2 { + left: 290px; + right: 140px; + height: 440px; + width: 10px; } .tic-tac-toe label { - background-color: #78bec5; height: 140px; width: 140px; display: none; - margin: 5px; + opacity: 0.0; position: absolute; cursor: pointer; - color: #fff; -moz-transition: background-color 0.3s; -o-transition: background-color 0.3s; -webkit-transition: background-color 0.3s; transition: background-color 0.3s; } +.tic-tac-toe label:hover { + opacity: 0.2; +} + +.circle { + position: absolute; + height: 100px; + width: 100px; + margin: 10px; + border: 10px solid #dc685a; + border-radius: 50%; +} + +.cross { + position: absolute; + width: 140px; + height: 140px; + display: block; + transform:rotate(45deg); + -ms-transform:rotate(45deg); + -webkit-transform:rotate(45deg); +} + +.cross:before, .cross:after { + content: ""; + position: absolute; + background: #78bec5; + border-radius: 3px; +} + +.cross:before { + top: 65px; + bottom: 65px; + left: 0px; + right: 0px; + height: 10px; + width: 140px; +} + +.cross:after { + left: 65px; + right: 65px; + height: 140px; + width: 10px; +} + .tic-tac-toe input.col-0 + label { left: 0px; right: 300px; @@ -68,7 +147,7 @@ body { {% for turn in turns_player %} .tic-tac-toe input.turn-{{turn}}:checked + label { cursor: default; - background-color: red; + opacity: 1.0; z-index: 10 !important; } @@ -77,14 +156,73 @@ body { display: block; } {% endfor %} + {% for m in moves %} .tic-tac-toe {% for p in m.ps %}input.turn-{{p.t}}.field-{{p.i}}:checked ~ {% endfor %} input.turn-{{m.n.t}}.field-{{m.n.i}} + label { display: block; cursor: default; - background-color: green; + opacity: 1.0; z-index: 10 !important; } {% endfor %} + +.tic-tac-toe .end { + width: 440px; + height: 440px; + padding-top: 125px; + position: absolute; + display: none; + background-color: #ecaf4f; + color: #3d4250; + text-align: center; + z-index: 11; + background: rgba(255, 255, 255, 0.8); + border-radius: 3px; +} + +.tic-tac-toe .end h1 { + margin: 40px; +} + +.tic-tac-toe .end a { + background-color: #3d4250; + border-radius: 3px; + text-decoration: none; + color: #fff; + padding: 10px; +} + +{% for m in wins2 %} +.tic-tac-toe +{% for p in m.ps %}input.turn-{{p.t}}.field-{{p.i}}:checked ~ {% endfor %} +.end { + display: block; + z-index: 10 !important; +} + +.tic-tac-toe +{% for p in m.ps %}input.turn-{{p.t}}.field-{{p.i}}:checked ~ {% endfor %} +.end > h1:before { + content: "Bot wins!" !important; +} + +{% endfor %} + +{% for m in draws %} +.tic-tac-toe +{% for p in m.ps %}input.turn-{{p.t}}.field-{{p.i}}:checked ~ {% endfor %} +.end { + display: block; + z-index: 10 !important; +} + +.tic-tac-toe +{% for p in m.ps %}input.turn-{{p.t}}.field-{{p.i}}:checked ~ {% endfor %} +.end > h1:before { + content: "Tied!" !important; +} + +{% endfor %} diff --git a/template.tictactoe.html b/template.tictactoe.html index d81c92d..e264af9 100644 --- a/template.tictactoe.html +++ b/template.tictactoe.html @@ -2,18 +2,33 @@