Lab 11: GoFirst and Nim 12:00 PM, Nov 19, 2017

Similar documents
(Provisional) Lecture 31: Games, Round 2

Project 4: Game Due: 7:00 PM, Dec 6, 2017

Tutorial 1. (ii) There are finite many possible positions. (iii) The players take turns to make moves.

Homework 7: Subsets Due: 10:00 PM, Oct 24, 2017

game tree complete all possible moves

Math 152: Applicable Mathematics and Computing

Lecture 33: How can computation Win games against you? Chess: Mechanical Turk

CS 188: Artificial Intelligence Spring Game Playing in Practice

Grade 7/8 Math Circles Game Theory October 27/28, 2015

CS 440 / ECE 448 Introduction to Artificial Intelligence Spring 2010 Lecture #5

V. Adamchik Data Structures. Game Trees. Lecture 1. Apr. 05, Plan: 1. Introduction. 2. Game of NIM. 3. Minimax

ADVERSARIAL SEARCH. Today. Reading. Goals. AIMA Chapter Read , Skim 5.7

CS 188: Artificial Intelligence Spring Announcements

CPSC 217 Assignment 3 Due Date: Friday March 30, 2018 at 11:59pm

Outline. Game Playing. Game Problems. Game Problems. Types of games Playing a perfect game. Playing an imperfect game

ADVERSARIAL SEARCH. Today. Reading. Goals. AIMA Chapter , 5.7,5.8

ADVERSARIAL SEARCH. Chapter 5

CS 2710 Foundations of AI. Lecture 9. Adversarial search. CS 2710 Foundations of AI. Game search

Programming Project 1: Pacman (Due )

CS 380: ARTIFICIAL INTELLIGENCE ADVERSARIAL SEARCH. Santiago Ontañón

Board Game AIs. With a Focus on Othello. Julian Panetta March 3, 2010

Game-Playing & Adversarial Search

Announcements. CS 188: Artificial Intelligence Spring Game Playing State-of-the-Art. Overview. Game Playing. GamesCrafters

CS 188: Artificial Intelligence

Adversarial Search and Game- Playing C H A P T E R 6 C M P T : S P R I N G H A S S A N K H O S R A V I

CS510 \ Lecture Ariel Stolerman

Adversarial Search Lecture 7

CS 380: ARTIFICIAL INTELLIGENCE

Adversarial Search. Hal Daumé III. Computer Science University of Maryland CS 421: Introduction to Artificial Intelligence 9 Feb 2012

2359 (i.e. 11:59:00 pm) on 4/16/18 via Blackboard

Playing Games. Henry Z. Lo. June 23, We consider writing AI to play games with the following properties:

CS 1571 Introduction to AI Lecture 12. Adversarial search. CS 1571 Intro to AI. Announcements

Game playing. Chapter 5. Chapter 5 1

A Brief Introduction to Game Theory

Game Playing. Philipp Koehn. 29 September 2015

Conversion Masters in IT (MIT) AI as Representation and Search. (Representation and Search Strategies) Lecture 002. Sandro Spina

CHECKMATE! A Brief Introduction to Game Theory. Dan Garcia UC Berkeley. The World. Kasparov

For slightly more detailed instructions on how to play, visit:

Game Playing AI Class 8 Ch , 5.4.1, 5.5

Artificial Intelligence Adversarial Search

Figure 1: A Checker-Stacks Position

Game Playing. Garry Kasparov and Deep Blue. 1997, GM Gabriel Schwartzman's Chess Camera, courtesy IBM.

Adversarial Search (Game Playing)

Artificial Intelligence, CS, Nanjing University Spring, 2018, Yang Yu. Lecture 4: Search 3.

Unit 12: Artificial Intelligence CS 101, Fall 2018

Adversary Search. Ref: Chapter 5

Game Tree Search. CSC384: Introduction to Artificial Intelligence. Generalizing Search Problem. General Games. What makes something a game?

CS 771 Artificial Intelligence. Adversarial Search

Contents. MA 327/ECO 327 Introduction to Game Theory Fall 2017 Notes. 1 Wednesday, August Friday, August Monday, August 28 6

CS885 Reinforcement Learning Lecture 13c: June 13, Adversarial Search [RusNor] Sec

Game Playing: Adversarial Search. Chapter 5

Set 4: Game-Playing. ICS 271 Fall 2017 Kalev Kask

Practice Midterm Exam 5

Game 0: One Pile, Last Chip Loses

COMP219: COMP219: Artificial Intelligence Artificial Intelligence Dr. Annabel Latham Lecture 12: Game Playing Overview Games and Search

Announcements. CS 188: Artificial Intelligence Fall Local Search. Hill Climbing. Simulated Annealing. Hill Climbing Diagram

Games vs. search problems. Game playing Chapter 6. Outline. Game tree (2-player, deterministic, turns) Types of games. Minimax

CS 4700: Foundations of Artificial Intelligence

Game playing. Chapter 6. Chapter 6 1

Game Theory and Algorithms Lecture 19: Nim & Impartial Combinatorial Games

Game playing. Chapter 6. Chapter 6 1

CS 188: Artificial Intelligence Spring 2007

CSE 573: Artificial Intelligence Autumn 2010

Adversarial search (game playing)

CPS 570: Artificial Intelligence Two-player, zero-sum, perfect-information Games

CSC 110 Lab 4 Algorithms using Functions. Names:

Outline. Game playing. Types of games. Games vs. search problems. Minimax. Game tree (2-player, deterministic, turns) Games

Sequential games. We may play the dating game as a sequential game. In this case, one player, say Connie, makes a choice before the other.

CS151 - Assignment 2 Mancala Due: Tuesday March 5 at the beginning of class

Games (adversarial search problems)

Adversarial Search Aka Games

Game playing. Chapter 5, Sections 1 6

CS 188: Artificial Intelligence. Overview

Analyzing Games: Solutions

Game Tree Search 1/6/17

Announcements. Homework 1. Project 1. Due tonight at 11:59pm. Due Friday 2/8 at 4:00pm. Electronic HW1 Written HW1

Today. Types of Game. Games and Search 1/18/2010. COMP210: Artificial Intelligence. Lecture 10. Game playing

ARTIFICIAL INTELLIGENCE (CS 370D)

Adversarial Search: Game Playing. Reading: Chapter

Local Search. Hill Climbing. Hill Climbing Diagram. Simulated Annealing. Simulated Annealing. Introduction to Artificial Intelligence

Artificial Intelligence

Tangent: Boromean Rings. The Beer Can Game. Plan. A Take-Away Game. Mathematical Games I. Introduction to Impartial Combinatorial Games

CS440/ECE448 Lecture 9: Minimax Search. Slides by Svetlana Lazebnik 9/2016 Modified by Mark Hasegawa-Johnson 9/2017

Game playing. Outline

Game Tree Search. Generalizing Search Problems. Two-person Zero-Sum Games. Generalizing Search Problems. CSC384: Intro to Artificial Intelligence

HW4: The Game of Pig Due date: Tuesday, Mar 15 th at 9pm. Late turn-in deadline is Thursday, Mar 17th at 9pm.

16.410/413 Principles of Autonomy and Decision Making

The game of Reversi was invented around 1880 by two. Englishmen, Lewis Waterman and John W. Mollett. It later became

Games CSE 473. Kasparov Vs. Deep Junior August 2, 2003 Match ends in a 3 / 3 tie!

CS 331: Artificial Intelligence Adversarial Search II. Outline

Game Playing AI. Dr. Baldassano Yu s Elite Education

CSC384: Introduction to Artificial Intelligence. Game Tree Search

CSE 3401 Assignment 4 Winter Date out: March 26. Date due: April 6, at 11:55 pm

COMP219: Artificial Intelligence. Lecture 13: Game Playing

Combined Games. Block, Alexander Huang, Boao. icamp Summer Research Program University of California, Irvine Irvine, CA

Ar#ficial)Intelligence!!

Artificial Intelligence. Minimax and alpha-beta pruning

Intro to Java Programming Project

Game-playing AIs: Games and Adversarial Search I AIMA

Introduction to Artificial Intelligence CS 151 Programming Assignment 2 Mancala!! Due (in dropbox) Tuesday, September 23, 9:34am

Transcription:

CS17 Integrated Introduction to Computer Science Hughes Contents Lab 11: GoFirst and Nim 12:00 PM, Nov 19, 2017 1 Prologue 1 2 Game Theory 1 3 Game Signature 2 4 GoFirst, A Game Module 3 5 Nim, A Game Module 4 6 Let s Play! 4 7 AI 5 1 Prologue While their owners are off learning magic and battling Voldemort, Hedwig and Pigwidgeon decide to pass the time by playing some snazzy terminal games developed just for them. Unfortunately, they are owls, and most owls do not know how to program. Hedwig asks you to help them by writing two games for them to play: GoFirst and Number in the Middle (Nim). Before we begin please copy over all of the files from /course/cs017/src/lab11 2 Game Theory For your Game project, you will implement a two-player, sequential, finite-action, deterministic, zero-sum game of perfect information. Let s define what that means: ˆ A sequential game is one in which only one player moves at a time. Monopoly is a sequential game; Rochambeau (i.e., rock-paper-scissors) is not. ˆ A finite-action game is one in which there is a finite number of legal moves available to a player when it is their turn to move. Battleship is a finite-action game; soccer is not. ˆ A deterministic game is one that does not depend at all on chance. Its progress is entirely a function of selected moves. Checkers is deterministic; backgammon is not. ˆ A zero-sum game is one in which what is good for one player is equally bad for the other, and vice versa. All the examples in this section are zero-sum games.

ˆ A game of perfect information is one in which both players witness the entire progression of the game. Chess is a game of perfect information; poker is not. In addition to implementing a game, you will also write a general purpose, artificially intelligent (AI) player that can play any game of this sort. Your AI player will solve a game using the minimax algorithm, together with a helper procedure that estimates the values of intermediate game states. GoFirst and Nim are both examples of two-player, sequential, finite-action, deterministic, zero-sum games of perfect information. GoFirst is essentially Tic-tac-toe on a one-by-one grid. On each turn, a player has the option to either go or pass. As you may have guessed, because there is only one space to fill, the first player to play go will win. For the CS 17 version of Nim, the players take turns removing matches from a pile of 21 matches. On each turn, the player whose turn it is takes one, two, or three matches. The player who takes the last match loses. In this lab, you will implement simple versions of GoFirst and Nim, with an AI player that doesn t even need to use minimax to play intelligently. 3 Game Signature First, you will design a game abstract data type (ADT). Your ADT will capture (much of) what is common to two-person, sequential, finite-action, deterministic, zero-sum games of perfect information. Game Signature Here is a start at a GAME signature: module type GAME = sig (* TYPES *) type which_player type state type move (* INITIAL VALUE *) val initial_state : state (* GAME LOGIC *) val legal_moves : state - > move list val next_state : state - > move - > state val estimate_value : state - > float... end This signature has three main components: ˆ Types: What does a player look like? What about a state, or a move? 2

player: Names the two players. state: Expresses the states of the game. What does the board look like, and whose turn is it? move: Describes the game s moves. ˆ Constructor: How do you construct a game? Specifically, what is the initial game state, before any moves have been made? initial_state: The initial game state. ˆ Game logic: These are the operations that define your game. legal_moves: Produces all the legal moves at a given state. next_state: Given a state and a move, produces the state that results from making that move. estimate_value: Associated with each state in a zero-sum game is a value. However, it is not always possible to compute such a value. For example, the value of the initial state in a two-player, zero-sum game is either + (a win for P1) or (a win for P2), but in Chess, this value is unknown. The procedure estimate_value computes an estimate of how good each state is. The lecture notes will more clearly explain this concept and how to approach writing this method. Your first task in this lab is to extend this signature to include other useful features, such as a way of prettily printing a game. For instance, in order to print out a specific player you could use val string_of_player : which_player ->string. Task: Add string_of_player, string_of_state, string_of_move, and move_of_string to your game signature with the correct types. 4 GoFirst, A Game Module Your next task is to write a module that implements this signature. implement the game of GoFirst. Here s a road map for how you might proceed: Specifically, you should 1. Write a template for a module that implements a testable version of GoFirst, starting like this: module ConcreteGoFirst = struct type which_player = P1 P2 type status = Win of which_player Draw Ongoing of which_player 3

type state =... type move =...... end For now, leave off the signature ascription, meaning : GAME, so that your code will compile. 2. Fill in the types for state and move. We have filled in the player and status types for you. What are the states in a game of GoFirst? To answer this, think about what you would need to record if you were in the middle of a game of GoFirst, and you intended to take a break and finish the game later. Your state type should encapsulate just these things. Once you have determined what is needed to represent a state in GoFirst, you can then go on to determine how to describe legal moves. That is, what will transition the game from one state to the next? 3. Add stubs for all of the procedures in the GAME signature. A stub is just a procedure header plus a body which does nothing useful, or reports an error. For example, here s a stub for the legal_moves procedure: let legal_moves a_state = failwith "not implemented" 4. The ConcreteGoFirst module is concrete. You should leave it as such so that your helpers are fully visible, and you can test them. But in addition, you should create a fully abstract GoFirst module, right below the definition of ConcreteGoFirst as follows: module GoFirst = (ConcreteGoFirst : GAME) ;; This will ensure that your GoFirst module implements your game signature. 5. Gradually fill in all of the stubs you just wrote until they re working. At this point, two human players should be able to play your game. (See Section 6 for instructions.) You ve reached a checkpoint! Please call over a lab TA to review your work. 5 Nim, A Game Module Now that you are done with GoFirst, Hedwig wants you to move on to Nim! Follow the same steps as in the section directly above to write another module that implements the GAME signature. This time however, you will implement the game of Nim in the provided /course/cs017/src/lab11/nim.ml file. Hint: When writing estimate_value, think about in what cases you are guaranteed to either win or to lose. For instance, if you have 4 matches, is there any case when you can win? 4

You ve reached a checkpoint! Please call over a lab TA to review your work. 6 Let s Play! Here is a player s signature. All a player does is choose its next move. module type PLAYER = sig (* given a state, and decides which legal move to make *) val next_move : Nim.state - > Nim.move end ;; There are two kinds of players in this world: human players and AI players. Both can be found in the /course/cs017/src/lab11/players.ml file. 1. The HumanPlayer makes its moves based on user input. We ve written most of this player for you because it uses some imperative language constructs. 1 Make the changes described in the comments to get it working! 2. The AIPlayer makes its moves based on artificial intelligence, which you will code soon. The Referee module is in charge of running games. It does so by constructing a HumanPlayer and a AIPlayer. It then lets you choose which kind of game to run. (* The Referee coordinates the other modules. * Invoke play_game to start playing a game. *) module Referee = struct (* Change these module names to what you've named them *) module Game = SimpleGame module Human : PLAYER with module Game := Game = HumanPlayer module AI : PLAYER with module Game := Game = AIPlayer... let play_game () = play... end The following line of code starts a game: Referee.play_game() Task: Play a few games of Nim against your partner. 1 By the end of CS 18, you will be able to write a human player all by yourself! 5

7 AI Annoyed by Hedwig winning all their games, Pigwidgeon wants a program that will help him always win. It is now up to you to write an AI that can defeat Hedwig. One nice thing about our simplified version of Nim is that there is a way to determine whether the current player will win or lose based solely on the number of matches left in the pile. What is this formula? Task: Play Nim a few times and try to discover this formula. (Call over the TAs if you need help.) Once you have the formula, you can define a perfect next_move procedure: i.e., one that will always win if possible. Hint: Don t forget that you can use estimate_value to estimate how good a specific state within the game is. Task: Write an AIPlayer module that implements the PLAYER signature and always plays an optimal move. Hint: Don t worry! This is way easier than it seems. See the comments in the players.ml file to see exactly what you have to do. Once a lab TA signs off on your work, you ve finished the lab! Congratulations! Before you leave, make sure both partners have access to the code you ve just written. Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS17document by filling out the anonymous feedback form: http://cs.brown.edu/ courses/cs017/feedback. 6