Python辅导 | 实现井字棋
Academic Dishonesty
We will be checking your code against other submissions in the class and from the Internet for logical redundancy. If you copy someone else’s code and submit it with minor changes, we will know. We trust you all to submit your own work only; please don’t let us down. If you do, we will pursue the strongest consequences available to us.
Discussion Forum
Please be careful not to post spoilers. Please don’t post any code that is directly related to the assignments. However you are welcome and encouraged to discuss general ideas on the discussion forums. If you have any questions about this assignment you should post them in the discussion forums. Do not email individual teachers with questions.
Write a Python program that will ask the user for two integers a and b such that 0 < a < b. Next, the user will be prompted to enter divisors (one or more). All divisors d should be 0 < d <= b. The user should be prompted again until the input is suitable.
Finally, your program should output a matrix with the following elements.
Matrix elements should be separated by a space.
Here a few examples.
a b: 1 16
Divisors: 2 3
a b: 1 1
a b: 16 1
a b: 1 16
Divisors: 2 3
a b: -1 16
a b: 1 -16
a b: -1 -16
a b: 0 0
a b: 0 1
a b: 1 16
Divisors: 2 3
a b: 1 16
Divisors: 2 17
Divisors: 17 2
Divisors: 2 3
a b: 120 140
Divisors: 2 20 3
120 1 1 1
121 0 0 0
122 1 0 0
123 0 1 0
124 1 0 0
125 0 0 0
126 1 1 0
127 0 0 0
128 1 0 0
129 0 1 0
130 1 0 0
131 0 0 0
132 1 1 0
133 0 0 0
134 1 0 0
135 0 1 0
136 1 0 0
137 0 0 0
138 1 1 0
139 0 0 0
a b: 1 16
Divisors: 3 2
a b: 1 16
Divisors: 3 2 2 3
a b: 2 7
Divisors: 5 4
a b: 2 7
Divisors: 5
a b: 2 7
Divisors: -5
Divisors: 5
a b: 1 16
Divisors: 3 2 -2
Divisors: -3 2 -2
Divisors: -3 -2 -2
Divisors: 3 2 0
Divisors: 3 2
Weight: 30%
Academic Dishonesty
We will be checking your code against other submissions in the class and from the Internet for logical redundancy. If you copy someone else’s code and submit it with minor changes, we will know. We trust you all to submit your own work only; please don’t let us down. If you do, we will pursue the strongest consequences available to us.
Discussion Forum
Please be careful not to post spoilers. Please don’t post any code that is directly related to the assignments. However you are welcome and encouraged to discuss general ideas on the discussion forums. If you have any questions about this assignment you should post them in the discussion forums. Do not email individual teachers with questions.
Tic-tac-toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3 × 3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.
Implement the 3 x 3 tic-tac-toe game so that your program passes the following test cases. You may assume that a user always enters a valid move. At the end of the game, you should output the winner or None if no winner.
Here a few examples.
012345678X–> 40123X5678O–> 70123X56O8X–> 50123XX6O8O–> 80123XX6OOX–> 60123XXXOOO–> 201O3XXXOOX–> 301OXXXXOOWinner: X012345678X–> 0X12345678O–> 3X12O45678X–> 4X12OX5678O–> 5X12OXO678X–> 7X12OXO6X8O–> 2X1OOXO6X8X–> 1XXOOXO6X8Winner: X012345678X–> 801234567XO–> 70123456OXX–> 6012345XOXO–> 501234OXOXX–> 40123XOXOXO–> 3012OXOXOXX–> 10X2OXOXOXO–> 20XOOXOXOXX–> 0XXOOXOXOXWinner: X012345678X–> 0X12345678O–> 6X12345O78X–> 3X12X45O78O–> 7X12X45OO8X–> 4X12XX5OO8O–> 8X12XX5OOOWinner: O012345678X–> 40123X5678O–> 50123XO678X–> 60123XOX78O–> 201O3XOX78X–> 10XO3XOX78O–> 80XO3XOX7OWinner: O012345678X–> 0X12345678O–> 4X123O5678X–> 1XX23O5678O–> 2XXO3O5678X–> 7XXO3O56X8O–> 5XXO3OO6X8X–> 8XXO3OO6XXO–> 6XXO3OOOXXWinner: O012345678X–> 40123X5678O–> 3012OX5678X–> 10X2OX5678O–> 70X2OX56O8X–> 60X2OX5XO8O–> 20XOOX5XO8X–> 80XOOX5XOXO–> 0OXOOX5XOXX–> 5OXOOXXXOXWinner: None012345678X–> 70123456X8O–> 40123O56X8X–> 60123O5XX8O–> 80123O5XXOX–> 0X123O5XXOO–> 3X12OO5XXOX–> 5X12OOXXXOO–> 1XO2OOXXXOX–> 2XOXOOXXXOWinner: None
Weight: 20%
Academic Dishonesty
We will be checking your code against other submissions in the class and from the Internet for logical redundancy. If you copy someone else’s code and submit it with minor changes, we will know. We trust you all to submit your own work only; please don’t let us down. If you do, we will pursue the strongest consequences available to us.
Discussion Forum
Please be careful not to post spoilers. Please don’t post any code that is directly related to the assignments. However you are welcome and encouraged to discuss general ideas on the discussion forums. If you have any questions about this assignment you should post them in the discussion forums. Do not email individual teachers with questions.
Implement a replay function for the 3 x 3 tic-tac-toe game of question 1. All the moves by both players will be provided as a single string of the following form:
(r1,c1)->(r2,c2)->(r3,c3)-> …
Here (r1,c1) represents the 2D coordinate (r1=row, c1=column) of the first move, (r2,c2) the 2D coordinate of the second move, etc. For example, (1, 2) is equivalent to entering a 5. You may assume that a user always enters a valid move string.
Here a few examples.
Replay: (1,1)->(2,1)->(1,2)->(2,2)->(2,0)->(0,2)->(1,0)012345678X–> 40123X5678O–> 70123X56O8X–> 50123XX6O8O–> 80123XX6OOX–> 60123XXXOOO–> 201O3XXXOOX–> 301OXXXXOOWinner: XReplay: (0,0)->(1,0)->(1,1)->(1,2)->(2,1)->(0,2)->(0,1)012345678X–> 0X12345678O–> 3X12O45678X–> 4X12OX5678O–> 5X12OXO678X–> 7X12OXO6X8O–> 2X1OOXO6X8X–> 1XXOOXO6X8Winner: XReplay: (2,2)->(2,1)->(2,0)->(1,2)->(1,1)->(1,0)->(0,1)->(0,2)->(0,0)012345678X–> 801234567XO–> 70123456OXX–> 6012345XOXO–> 501234OXOXX–> 40123XOXOXO–> 3012OXOXOXX–> 10X2OXOXOXO–> 20XOOXOXOXX–> 0XXOOXOXOXWinner: XReplay: (0,0)->(2,0)->(1,0)->(2,1)->(1,1)->(2,2)012345678X–> 0X12345678O–> 6X12345O78X–> 3X12X45O78O–> 7X12X45OO8X–> 4X12XX5OO8O–> 8X12XX5OOOWinner: OReplay: (1,1)->(1,2)->(2,0)->(0,2)->(0,1)->(2,2)012345678X–> 40123X5678O–> 50123XO678X–> 60123XOX78O–> 201O3XOX78X–> 10XO3XOX78O–> 80XO3XOX7OWinner: OReplay: (0,0)->(1,1)->(0,1)->(0,2)->(2,1)->(1,2)->(2,2)->(2,0)012345678X–> 0X12345678O–> 4X123O5678X–> 1XX23O5678O–> 2XXO3O5678X–> 7XXO3O56X8O–> 5XXO3OO6X8X–> 8XXO3OO6XXO–> 6XXO3OOOXXWinner: OReplay: (1,1)->(1,0)->(0,1)->(2,1)->(2,0)->(0,2)->(2,2)->(0,0)->(1,2)012345678X–> 40123X5678O–> 3012OX5678X–> 10X2OX5678O–> 70X2OX56O8X–> 60X2OX5XO8O–> 20XOOX5XO8X–> 80XOOX5XOXO–> 0OXOOX5XOXX–> 5OXOOXXXOXWinner: NoneReplay: (2,1)->(1,1)->(2,0)->(2,2)->(0,0)->(1,0)->(1,2)->(0,1)->(0,2)012345678X–> 70123456X8O–> 40123O56X8X–> 60123O5XX8O–> 80123O5XXOX–> 0X123O5XXOO–> 3X12OO5XXOX–> 5X12OOXXXOO–> 1XO2OOXXXOX–> 2XOXOOXXXOWinner: None
Weight: 30%
Academic Dishonesty
We will be checking your code against other submissions in the class and from the Internet for logical redundancy. If you copy someone else’s code and submit it with minor changes, we will know. We trust you all to submit your own work only; please don’t let us down. If you do, we will pursue the strongest consequences available to us.
Discussion Forum
Please be careful not to post spoilers. Please don’t post any code that is directly related to the assignments. However you are welcome and encouraged to discuss general ideas on the discussion forums. If you have any questions about this assignment you should post them in the discussion forums. Do not email individual teachers with questions.
Implement the Tic-tac-toe game for variable board sizes, you may assume: 2 < s < 11, where s is the board size. Before the game starts the program will prompt for the board size.
Here are a few sample runs. The output is a bit different so that we can handle two-digit coordinates consistently. We expect (but you won’t lose any marks if you don’t) that you make extensive use of compound data types and string library. Note that our solution has less than 50 lines of code. You may assume that a user always enters a valid move.
Here a few examples.
Size–> 6 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 1 0 X 2 3 4 5 6 7 8 9 10 1112 13 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35O–> 13 0 X 2 3 4 5 6 7 8 9 10 1112 O 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 2 0 X X 3 4 5 6 7 8 9 10 1112 O 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35O–> 14 0 X X 3 4 5 6 7 8 9 10 1112 O O 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 3 0 X X X 4 5 6 7 8 9 10 1112 O O 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35O–> 15 0 X X X 4 5 6 7 8 9 10 1112 O O O 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 4 0 X X X X 5 6 7 8 9 10 1112 O O O 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35O–> 16 0 X X X X 5 6 7 8 9 10 1112 O O O O 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 5 0 X X X X X 6 7 8 9 10 1112 O O O O 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35O–> 17 0 X X X X X 6 7 8 9 10 1112 O O O O O18 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 6 0 X X X X X X 7 8 9 10 1112 O O O O O18 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35O–> 18 0 X X X X X X 7 8 9 10 1112 O O O O O O 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 0 X X X X X X X 7 8 9 10 1112 O O O O O O 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35Winner: XSize–> 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 7172 73 74 75 76 77 78 79 80X–> 0 X 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 7172 73 74 75 76 77 78 79 80O–> 72 X 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O 73 74 75 76 77 78 79 80X–> 1 X X 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O 73 74 75 76 77 78 79 80O–> 73 X X 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 79 80X–> 8 X X 2 3 4 5 6 7 X 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 79 80O–> 80 X X 2 3 4 5 6 7 X 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 79 OX–> 7 X X 2 3 4 5 6 X X 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 79 OO–> 79 X X 2 3 4 5 6 X X 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 O OX–> 2 X X X 3 4 5 6 X X 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 O OO–> 74 X X X 3 4 5 6 X X 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O O 75 76 77 78 O OX–> 3 X X X X 4 5 6 X X 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O O 75 76 77 78 O OO–> 75 X X X X 4 5 6 X X 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O O O 76 77 78 O OX–> 4 X X X X X 5 6 X X 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O O O 76 77 78 O OO–> 76 X X X X X 5 6 X X 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O O O O 77 78 O OX–> 5 X X X X X X 6 X X 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O O O O 77 78 O OO–> 77 X X X X X X 6 X X 9 10 11 12 13 14 15 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O O O O O 78 O OX–> 15 X X X X X X 6 X X 9 10 11 12 13 14 X 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O O O O O 78 O OO–> 78 X X X X X X 6 X X 9 10 11 12 13 14 X 16 1718 19 20 21 22 23 24 25 2627 28 29 30 31 32 33 34 3536 37 38 39 40 41 42 43 4445 46 47 48 49 50 51 52 5354 55 56 57 58 59 60 61 6263 64 65 66 67 68 69 70 71 O O O O O O O O OWinner: OSize–> 6 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 0 X 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35O–> 1 X O 2 3 4 5 6 7 8 9 10 1112 13 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 2 X O X 3 4 5 6 7 8 9 10 1112 13 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35O–> 3 X O X O 4 5 6 7 8 9 10 1112 13 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 4 X O X O X 5 6 7 8 9 10 1112 13 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35O–> 5 X O X O X O 6 7 8 9 10 1112 13 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 12 X O X O X O 6 7 8 9 10 11 X 13 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35O–> 13 X O X O X O 6 7 8 9 10 11 X O 14 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 14 X O X O X O 6 7 8 9 10 11 X O X 15 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35O–> 15 X O X O X O 6 7 8 9 10 11 X O X O 16 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 16 X O X O X O 6 7 8 9 10 11 X O X O X 1718 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35O–> 17 X O X O X O 6 7 8 9 10 11 X O X O X O18 19 20 21 22 2324 25 26 27 28 2930 31 32 33 34 35X–> 24 X O X O X O 6 7 8 9 10 11 X O X O X O18 19 20 21 22 23 X 25 26 27 28 2930 31 32 33 34 35O–> 25 X O X O X O 6 7 8 9 10 11 X O X O X O18 19 20 21 22 23 X O 26 27 28 2930 31 32 33 34 35X–> 26 X O X O X O 6 7 8 9 10 11 X O X O X O18 19 20 21 22 23 X O X 27 28 2930 31 32 33 34 35O–> 27 X O X O X O 6 7 8 9 10 11 X O X O X O18 19 20 21 22 23 X O X O 28 2930 31 32 33 34 35X–> 28 X O X O X O 6 7 8 9 10 11 X O X O X O18 19 20 21 22 23 X O X O X 2930 31 32 33 34 35O–> 29 X O X O X O 6 7 8 9 10 11 X O X O X O18 19 20 21 22 23 X O X O X O30 31 32 33 34 35X–> 11 X O X O X O 6 7 8 9 10 X X O X O X O18 19 20 21 22 23 X O X O X O30 31 32 33 34 35O–> 10 X O X O X O 6 7 8 9 O X X O X O X O18 19 20 21 22 23 X O X O X O30 31 32 33 34 35X–> 9 X O X O X O 6 7 8 X O X X O X O X O18 19 20 21 22 23 X O X O X O30 31 32 33 34 35O–> 8 X O X O X O 6 7 O X O X X O X O X O18 19 20 21 22 23 X O X O X O30 31 32 33 34 35X–> 7 X O X O X O 6 X O X O X X O X O X O18 19 20 21 22 23 X O X O X O30 31 32 33 34 35O–> 6 X O X O X O O X O X O X X O X O X O18 19 20 21 22 23 X O X O X O30 31 32 33 34 35X–> 23 X O X O X O O X O X O X X O X O X O18 19 20 21 22 X X O X O X O30 31 32 33 34 35O–> 22 X O X O X O O X O X O X X O X O X O18 19 20 21 O X X O X O X O30 31 32 33 34 35X–> 21 X O X O X O O X O X O X X O X O X O18 19 20 X O X X O X O X O30 31 32 33 34 35O–> 20 X O X O X O O X O X O X X O X O X O18 19 O X O X X O X O X O30 31 32 33 34 35X–> 19 X O X O X O O X O X O X X O X O X O18 X O X O X X O X O X O30 31 32 33 34 35O–> 18 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O30 31 32 33 34 35X–> 30 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X 31 32 33 34 35O–> 31 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O 32 33 34 35X–> 32 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O X 33 34 35O–> 33 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O X O 34 35X–> 34 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O X O X 35O–> 35 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O X O X OWinner: None