Principle of Computing (Python)学习笔记(3) probability +Objects and reference + tic_tac_toe
2014-07-11 06:29
495 查看
1 Basic Probability
https://class.coursera.org/principlescomputing-001/wiki/view?page=probability
http://www.codeskulptor.org/#poc_basic_probability.py
http://www.codeskulptor.org/#poc_love_songs.py
http://www.codeskulptor.org/#examples_monte_carlo.py
https://class.coursera.org/principlescomputing-001/wiki/tictactoemc
2 Objects and references
In Python, Everything is an object.
In Java, Not Everything is an object, some are primitive types.
Objects are like the person Rixner,it is really a person.
(Variables)Reference are like the name “Rixner”, it is a name, which is used to call the person.
A variable is just a reference, and that the data itself is inside of an object.
3 tic-tac-toe
参考资料
https://class.coursera.org/principlescomputing-001/wiki/grids
https://class.coursera.org/principlescomputing-001/wiki/TTTBoard
http://www.codeskulptor.org/#poc_ttt_provided.py
http://www.codeskulptor.org/#poc_ttt_template.py
http://www.codeskulptor.org/#poc_simpletest.py
我的作业
https://class.coursera.org/principlescomputing-001/wiki/view?page=probability
http://www.codeskulptor.org/#poc_basic_probability.py
http://www.codeskulptor.org/#poc_love_songs.py
http://www.codeskulptor.org/#examples_monte_carlo.py
https://class.coursera.org/principlescomputing-001/wiki/tictactoemc
2 Objects and references
In Python, Everything is an object.
In Java, Not Everything is an object, some are primitive types.
Objects are like the person Rixner,it is really a person.
(Variables)Reference are like the name “Rixner”, it is a name, which is used to call the person.
A variable is just a reference, and that the data itself is inside of an object.
3 tic-tac-toe
参考资料
https://class.coursera.org/principlescomputing-001/wiki/grids
https://class.coursera.org/principlescomputing-001/wiki/TTTBoard
http://www.codeskulptor.org/#poc_ttt_provided.py
http://www.codeskulptor.org/#poc_ttt_template.py
http://www.codeskulptor.org/#poc_simpletest.py
我的作业
""" Monte Carlo Tic-Tac-Toe Player """ import random import poc_ttt_gui import poc_ttt_provided as provided # Constants for Monte Carlo simulator # Change as desired NTRIALS = 100 # Number of trials to run MCMATCH = 1.0 # Score for squares played by the machine player MCOTHER = 1.0 # Score for squares played by the other player #Players are PLAYERX and PLAYERO. def mc_trial(board, player): """ This function runs a single random trial with the game board. """ while board.check_win() == None: empty = board.get_empty_squares() choice = random.choice(empty) board.move(choice[0], choice[1], player) if player == 2: player = 3 else: player = 2 return None def mc_update_scores(scores, board, player): """ This function updates the scoreboard based on which moves worked and which didn't. """ winner = board.check_win() if winner == 4: # if it's a draw, don't score return None for row in range(0,board.get_dim()): for col in range(0,board.get_dim()): position = board.square(row, col) if position == 2: # player x, or computer if winner == 2: scores[row][col] += MCMATCH else: scores[row][col] -= MCMATCH elif position == 3: #player o, or other if winner == 3: scores[row][col] += MCOTHER else: scores[row][col] -= MCOTHER def get_best_move(board, scores): """ This function picks the best move from a list of possible choices. """ empty = board.get_empty_squares() choose = [] max_score = None for tile in range(0, len(empty)): row = empty[tile][0] col = empty[tile][1] if scores[row][col] > max_score: choose = [] choose.append((empty[tile][0], empty[tile][1])) max_score = scores[row][col] if scores[row][col] == max_score: choose.append((empty[tile][0], empty[tile][1])) return random.choice(choose) def mc_move(board, player, trials): """ When this function is called, it submits the machine's move. """ scores = [[0 for dummy_row in range(0, board.get_dim())] for dummy_col in range(0, board.get_dim())] for dummy_trial in range(0, trials): trial_board = board.clone() mc_trial(trial_board, player) mc_update_scores(scores, trial_board, player) move = get_best_move(board, scores) return (move[0], move[1]) provided.play_game(mc_move, NTRIALS, False) poc_ttt_gui.run_gui(3, provided.PLAYERX, mc_move, NTRIALS, False)
相关文章推荐
- Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
- Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
- Principle of Computing (Python)学习笔记(6) Recursion + Word Wrangler
- Principle of Computing (Python)学习笔记(4) Combination + Yahtzee
- Principle of Computing (Python)学习笔记(1) 2048
- Principle of Computing (Python)学习笔记(5) BFS Searching + Zombie Apocalypse
- Principle of Computing (Python)学习笔记(1) 2048
- Principle of Computing (Python)学习笔记(5) BFS Searching + Zombie Apocalypse
- Introduction to Languages and the Theory of Computation--Chapter 1 学习笔记
- 学习笔记之Object Reference Conversion and Casting
- Python学习笔记(7)Mouse Input, More Lists and Dictionaries
- Python 学习笔记(一):numbers and expressions
- Python 学习笔记(二):List and Tuple
- MSDN Kernel-Mode Driver Architecture学习笔记(3)——Device Objects and Device Stacks (2)
- Andrew Ng机器学习公开课笔记 -- Mixtures of Gaussians and the EM algorithm
- 使用Python编写一个简单的tic-tac-toe游戏的教程
- Byte of Python学习笔记一
- Byte of Python学习笔记八
- python tutorial 学习笔记(七)Errors and Exceptions
- Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记四