You plan you 1D tic-tac-toe on a board that has one row with 20 spaces.
There are two players.
The first player makes a move by places an x
into an empty space.
The second player places an o
. Then the first player plays again.
For example:
-------x------------
-------x--o---------
-------xx-o---------
-------xxoo---------
------xxxoo---------
The player who places three of their own marks next to each other wins.
Write a function evaluate
that gets the string with the board of 1D tic-tac-toe,
and returns one character based on the state of the game:
"x"
– The player who uses crosses (Xs) has won (the board contains xxx
)"o"
– The player who uses noughts (Os) has won (the board contains ooo
)"!"
– Draw (the board is full but nobody has won)"-"
– Rest (i.e. the game is not finished)Write a move function that gets the string with the game board, a position number (0-19), and a (x or o) mark, and returns a game board (i.e., a string with the given mark placed in the given position). The function header should look something like this:
def move(board, mark, position):
# Returns the game board with the given mark in the given position.
...
Write a player_move function that gets a string with the game board, asks the player which position he wants to play, and returns the updated game board with the player's move. The function should reject negative or too large numbers and moves to an occupied position. If the user has entered a wrong argument, the function should ask again.
Write a pc_move function that gets the string with the game board.
It will select a position to play, and returns
the game board with the computer's move.
Use a simple random "strategy":
def pc_move(board):
# Returns a game board with the computer's move.
...
Write a 1D_tictactoe function that creates a string with a game board and alternately calls the player_move and pc_move functions until someone wins or draws. Do not forget to check the status of the game after every turn.
{ "data": { "sessionMaterial": { "id": "session-material:2018/pyladies-en-prague:tod:1", "title": "Homework - 1D tic-tac-toe", "html": "\n \n \n\n <h1>One-dimensional tic-tac-toe</h1>\n<p>You plan you 1D tic-tac-toe on a board that has one row with 20 spaces.</p>\n<p>There are two players. \nThe first player makes a move by places an <code>x</code> into an empty space.\nThe second player places an <code>o</code>. Then the first player plays again.</p>\n<p>For example:</p>\n<ol>\n<li>move: <code>-------x------------</code></li>\n<li>move: <code>-------x--o---------</code></li>\n<li>move: <code>-------xx-o---------</code></li>\n<li>move: <code>-------xxoo---------</code></li>\n<li>move: <code>------xxxoo---------</code></li>\n</ol>\n<p>The player who places three of their own marks next to each other wins.</p>\n<h2>Assignment 1</h2>\n<p>Write a function <code>evaluate</code> that gets the string with the board of 1D tic-tac-toe,\nand returns one character based on the state of the game:</p>\n<ul>\n<li><code>"x"</code> – The player who uses crosses (Xs) has won (the board contains <code>xxx</code>)</li>\n<li><code>"o"</code> – The player who uses noughts (Os) has won (the board contains <code>ooo</code>)</li>\n<li><code>"!"</code> – Draw (the board is full but nobody has won)</li>\n<li><code>"-"</code> – Rest (i.e. the game is not finished)</li>\n</ul>\n<h2>Assignment 2</h2>\n<p>Write a move function that gets the string with the game board, a position number (0-19),\nand a (x or o) mark, and returns a game board (i.e., a string with the given mark placed in the given position).\nThe function header should look something like this:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">move</span><span class=\"p\">(</span><span class=\"n\">board</span><span class=\"p\">,</span> <span class=\"n\">mark</span><span class=\"p\">,</span> <span class=\"n\">position</span><span class=\"p\">):</span>\n <span class=\"c1\"># Returns the game board with the given mark in the given position.</span>\n <span class=\"o\">...</span>\n</pre></div><h2>Assignment 3</h2>\n<p>Write a player_move function that gets a string with the game board, asks the player \nwhich position he wants to play,\nand returns the updated game board with the player's move. The function should reject \nnegative or too large numbers and moves to an occupied position. If the user has entered\na wrong argument, the function should ask again.</p>\n<h2>Assignment 4</h2>\n<p>Write a pc_move function that gets the string with the game board.\nIt will select a position to play, and returns\nthe game board with the computer's move.<br>\nUse a simple random "strategy":</p>\n<ul>\n<li>Select a random number from 0 to 19.</li>\n<li>If the position is empty, place the computer's mark there.</li>\n<li>If not, repeat from the first step (select another random number).\nThe function header should look something like this:</li>\n</ul>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">pc_move</span><span class=\"p\">(</span><span class=\"n\">board</span><span class=\"p\">):</span>\n <span class=\"c1\"># Returns a game board with the computer's move.</span>\n <span class=\"o\">...</span>\n</pre></div><h2>Assignment 5</h2>\n<p>Write a 1D_tictactoe function that creates a string with a game board and alternately calls the player_move and\npc_move functions until someone wins or draws.\nDo not forget to check the status of the game after every turn.</p>\n<h5>Can you program a better strategy for your computer?</h5>\n\n\n " } } }