ECE15: Lab #4. Problem 1. University of California San Diego

Similar documents
ECE15: Lab #3. Problem 1. University of California San Diego ( 1) + x4. + x8 + (1)

Programming Assignment #4 Arrays and Pointers

CpSc 1111 Lab 5 Formatting and Flow Control

University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Final Exam

ECE15: Lab #2. Problem 1. University of California San Diego

CpSc 1111 Lab 9 2-D Arrays

CpSc 1111 Lab 4 Formatting and Flow Control

1. Introduction. 2. Project Submission and Deliverables

Introduction to Computer Science Midterm 3 Fall, Points

Q1: C input/output; operators / 46 Q2: Conditional statements / 34 Q3: While and do-while loops / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

CSE 5A Introduction to Programming I (C) Homework 4

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Tic Tac Toe Game! Day 8

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

3COM0271 Computer Network Protocols & Architectures A

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

SPRING 2017 CSCI 304 LAB1 (Due on Feb-14, 11:59:59pm)

EAS230: Programming for Engineers Lab 1 Fall 2004

CS 210 Fundamentals of Programming I Spring 2015 Programming Assignment 4

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Lab 1 Implementing a Simon Says Game

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

EECE.2160: ECE Application Programming Fall 2017

This sheet must be the cover page for every programming assignment. Total

CSE 333 Midterm Exam July 24, Name UW ID#

CS 150 Lab 10 Functions and Random Numbers

Session 8.2. Finding Winners Using Arrays

2005 Stanford Local Programming Contest

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2016 Midterm I

Crude Video Game Simulator Algorithm

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lab 4: Tracery Recursion in C with Linked Lists

Introduction to C Programming

EECE.2160: ECE Application Programming

CS 241 Data Organization using C Project 3: Tron Spring 2017

EECE.2160: ECE Application Programming Fall 2017

C Functions. 5.2 Program Modules in C

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

CMPSC 111 Introduction to Computer Science I Fall 2016 Lab 8 Assigned: October 26, 2016 Due: November 2, 2016 by 2:30pm

IT115: Introduction to Java Programming 1. IT115: Introduction to Java Programming. Tic Tac Toe Application. Trina VanderLouw

Informatics 2D. Coursework 1: Search and Games

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Decision Logic: if, if else, switch, Boolean conditions and variables

Programming Assignment - 1

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter Lab 8 - Structures

It is academic misconduct to share your work with others in any form including posting it on publicly accessible web sites, such as GitHub.

Blocky: A Game of Falling Blocks

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

LAB: WHILE LOOPS IN C++

CS 211 Programming Practicum Fall 2018

CS : Programming for Non-majors, Fall 2018 Programming Project #2: Census Due by 10:20am Wednesday September

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Programming Problems 15th Annual Computer Science Programming Contest

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Programming Style Guide v1.1

Functions. printf( %f cubed is %f,x,cube(x)); return 0; } return parameter type double cube(double var) { return (var*var*var);

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website:

Function Call Stack and Activation Records

Gradebook - Grades Tab Create Assignment

You will provide an implementation for a test driver and for a C function that satisfies the conditions stated in the header comment:

CS 101, Spring 2016 March 22nd Exam 2

CS : Programming for Non-majors, Summer 2007 Programming Project #2: Census Due by 12:00pm (noon) Wednesday June

C Introduction. Comparison w/ Java, Memory Model, and Pointers

EECE.2160: ECE Application Programming Spring 2018

do fifty-two: An Introductory Programming Language

Lecture 04 FUNCTIONS AND ARRAYS

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CP Lab 4: Simple graphics with descartes

LAB 4.1 Relational Operators and the if Statement

Project 1 - Battleship Game

Writing to and reading from files

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

Programming. C++ Basics

gcc o driver std=c99 -Wall driver.c bigmesa.c

Final Exam, 3/17/2016 CIS330, Winter 2016

Lab 1 Implementing a Simon Says Game

2004 Stanford Local Programming Contest

Saturday Morning Math Group Austin Math Circle Austin Area Problem Solving Challenge

CMSC 201 Spring 2018 Project 2 Battleship

CSCI 1301: Introduction to Computing and Programming Spring 2018 Project 3: Hangman 2.0 (A Word Guessing Game)

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Coding Standards for C

Grade Point Scales Standard Honors AP/College A B C D F Sample file

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

EECE.2160: ECE Application Programming Spring 2018 Programming Assignment #6: Using Arrays to Count Letters in Text Due Wednesday, 4/4/18, 11:59:59 PM

ROCK PAPER SCISSORS Rock Paper Scissors Lab Project Using C or C++

EECE.2160: ECE Application Programming Fall 2017 Exam 3 December 16, 2017

CSE 142, Autumn 2018 Programming Assignment #9: Critters (20 points) Due Tuesday, December 4th, 9:00 PM

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

OOP- 4 Templates & Memory Management Print Only Pages 1-5 Individual Assignment Answers To Questions 10 Points - Program 15 Points

Chapter 6 - Pointers

Transcription:

University of California San Diego ECE15: Lab #4 This lab is a cumulative wrap-up assignment for the entire course. As such, it relates to the material covered in Lecture Units 1 5 and 7 9 in class. Here are several instruction specific to this lab: In this lab, you are given the files connect4.c and connect4 functions.h, which you can download from the course website. The file connect4.c contains the main() function, while the file connect4 functions.h contains the forward declarations of the functions you are required to implement. You should implement these functions in the file connect4 functions.c, which is the only file you will be submitting in this lab. As a starting point, you should use the template for connect4 functions.c, provided on the course website at http://ece15.ucsd.edu/main/homeworks.html. Make sure that the file connect4 functions.c that you submit compiles and executes correctly with the files connect4.c and connect4 functions.h provided on the course website. You are not allowed to change (edit) these files in any way. In fact, you cannot change them: if you do, your changes will not really take effect. The first step in grading the file connect4 functions.c that you submit will consist of compiling it together with the original versions of connect4.c and connect4 functions.h. This lab includes an extra-credit component. In order to be eligible for extra credit, you must implement the function best move(). The function best move() that you submit will be tested to verify that it satisfies several conditions described in detail on page 10. All the best move() functions that pass these tests will play against each other in a round-robin competition, with the winner of the competition awarded 100 extra-credit points. In this lab, you can use any functions you like from the C standard library. The header files <stdio.h>, <stdlib.h>, <time.h>, and <stdbool.h> are already included in the file connect4 functions.h. If you would like to use standard library functions other than those declared in <stdio.h>, <stdlib.h>, <time.h>, you should #include the corresponding header files in the file connect4 functions.c. Try to make your code easy to follow and understand: choose meaningful names for identifiers, use proper indentation, and provide comments wherever they are useful. While this is not required, it could help you receive partial credit: C code that is not correct and is also difficult to understand will receive zero credit, even if it is partially correct. This lab will be graded out of 100 points, with up to 100 extra-credit points. You should submit the lab by following the instructions posted at http://ece15.ucsd.edu/labs/turnin4.html. Problem 1. The goal of this problem is to implement the Connect-Four game between a computer and a human player. Throughout this lab, we will refer to the human player as Alice for short. A brief description of the Connect-Four game follows. You can find much more information about this game on the web, for example at http://mathworld.wolfram.com/connect-four.html. You can actually play the game against a computer at www.mathsisfun.com/games/connect4.html.

Rules of the Game: Connect-Four is a two-player game played on a vertical board 7 columns across and 6 rows high. If you were to buy the game at a toy store, the board might look like this: Each player begins the game with 21 identical stones (alternatively called pieces, chips, tokens, etc.). The two players alternate in making moves. Throughout this lab, the stones of the player that makes the first move are marked by X, while the stones of the player that moves second are marked by O. Each move consists of a player dropping a stone into one of the seven columns. Because the board is vertical, stones dropped into a given column always slide down to the lowest unoccupied row of that column. A column that already contains 6 stones is full, and no other stones can be placed in this column (attempting to do so constitutes an invalid move). The objective of each player is to place four of his/her stones on the board so that they form a horizontal, vertical, or diagonal sequence. The first player that is able to do so wins, and the game stops at this point. If all the 7 6 = 42 stones have been placed on the board without either player winning, then the game is considered a draw. In this lab, the function main()that implements the Connect-Four game between Alice and the computer is given to you in the file connect4.c, which you should download from the course website at http://ece15.ucsd.edu/main/homeworks.html. As you will see (on the next page), the function main()calls five other functions, namely: print welcome(void) display board(int board[][board SIZE VERT]) random move(int board[][board SIZE VERT], int computer num) player move(int board[][board SIZE VERT], int player num) check win or tie(int board[][board SIZE VERT], int last move) You are required to implement these functions in the file connect4 functions.c. Note that forward declarations of these functions are also given to you, in the file connect4 functions.h which you should download from the course website. Therefore, you cannot change the function prototypes that is, the name of the function, its return type, and the type(s) of its parameter(s). Descriptions of these five functions follow shortly, but first here is the function main()from connect4.c. 2

int main() { int board[board SIZE HORIZ][BOARD SIZE VERT] = {{0}}; int player num, computer num; int last move; /* Ask Alice if she wants to go first */ player num = print welcome(); if (player num == 1) computer num = 2; else computer num = 1; /* If Alice wants to go first, let her make a move */ if (player num == 1) { display board(board); last move = player move(board,player num); display board(board); } /* The main loop */ while (1) { /* Make a computer move, then display the board */ last move = random move(board,computer num); printf("computer moved in column: %d\n", last move); display board(board); /* Check whether the computer has won */ if (check win or tie(board,last move)) return 0; /* Let Alice make a move, then display the board */ last move = player move(board,player num); display board(board); /* Check whether Alice has won */ if (check win or tie(board,last move)) return 0; } /* end of while (1) */ } /* end of main() */ Note that symbolic constants BOARD SIZE HORIZ and BOARD SIZE VERT are defined in the file connect4 functions.h as 7 and 6. Now, here is a description of the functions called by main(). 3

int print welcome(void) This function does not take any input. It prints a welcome message for Alice, and then asks her if she would like to make the first move. Here are a couple of examples: *** Welcome to the Connect Four game!!! *** Would you like to make the first move [y/n]: N *** Welcome to the Connect Four game!!! *** Would you like to make the first move [y/n]: y The function then reads from stdin the input typed by Alice, and clears the input buffer (in case she entered more than one character) using while (getchar()!= \n ). If she typed n or N, the function returns 2 indicating that Alice will be the second player to move. In all other cases, the function returns 1, giving Alice the advantage of the first move (the computer is a gentleman). void display board(int board[][board SIZE VERT]) This function receives (a pointer to) the board array as input, and then prints the current state of the board to stdout. The function expects the value of every cell in the board array to be either 0, 1, or 2, where 1 denotes stones of the first player (which should be printed as X) while 2 denotes stones of the second player (which should be printed as O). A board cell whose value is 0 corresponds to a place on the board that is not occupied by a stone of either player. The print-out of the board to stdout should be formatted as follows. The width of every cell is three characters, and the stone occupying this cell (if any) is the middle character. Vertical lines separate between the cells in the same row, while the rows themselves are separated by a line of hyphens along with + characters. Right under the board, the function should print the indices of the columns, with each such index centered in its column. Here is an example: O X O X X X X X O O O O X O X O X O int random move(int board[][board SIZE VERT], int computer num) This function receives (a pointer to) the board array and a player number (either 1 or 2) as input. It then makes a valid random move. To this end, the function should generate uniformly at random an integer m in the range 1,2,...,BOARD SIZE HORIZ using an (appropriately normalized) call to the rand() standard library function. It should then verify that this integer m constitutes a valid move, by calling the function is column full(). If m is a valid 4

move, the function should return m. If not (that is, if the m-th column is full), the function should repeat the process until a valid move is generated by rand(). Note that the function assumes that at least one cell in the array board is 0; otherwise it enters into an infinite loop! Prior to returning m, the function should also update the state of the board by making the function call update board(board,m,computer num). int player move(int board[][board SIZE VERT], int player num) This function receives (a pointer to) the board array and a player number (either 1 or 2) as input. The function prompts Alice to enter her move, reads from stdin the input entered by Alice, then clears the input buffer using while (getchar()!= \n ). If Alice enters anything other than an integer in the range 1,2,...,BOARD SIZE HORIZ, the function should say Not a valid move. Enter a column number!, then prompt Alice again to enter her move. If Alice enters an integer m in the range 1,2,...,BOARD SIZE HORIZ, the function should verify that the corresponding column is not full by calling the function is column full(). If the column is full, the function should say This column is full. Try again! and again prompt Alice to enter a move. If Alice does enter a valid move m, the function updates the state of the board via the call update board(board,m,player num), and then returns m. Here is an example of a call to display board() followed by a call to player move(): O O X X O X X X X X O O O O X O X O X O Please enter your move:??? Please enter your move: -5 3 Please enter your move: 4 This column is full. Try again! Please enter your move: 7 bool check win or tie(int board[][board SIZE VERT], int last move) This function receives as input (a pointer to) the board array and an integer last move, which is interpreted as the index of the column (in the range 1,2,...,BOARD SIZE HORIZ) where the most recent stone was played. The function calls check winner() to determine whether the game has been won by either player. If so, the function should print Player X won! or Player O won! and return true. If there is no winner, the function checks whether the game is a draw (no spaces left on the board). If so, the function should print Tie game! and return true. Otherwise, the function returns false, indicating that the game is not yet over. 5

You have probably noticed that (some of) the five functions described above call upon other functions, specifically the functions: is column full(int board[][board SIZE VERT], int m) update board(int board[][board SIZE VERT], int m, int player num) check winner(int board[][board SIZE VERT], int last move) This is a common practice in programming, wherein a given task is broken into smaller and smaller components. Concretely, this means that you are required to implement the three functions above, in the file connect4 functions.c. The forward declarations of these functions are already given in the header file connect4 functions.h. Here is their description. bool is column full(int board[][board SIZE VERT], int m) This function receives as input (a pointer to) the board array and an integer m which is expected to be in the range 1,2,...,BOARD SIZE HORIZ. The function returns true if the m-th column of the board is full (already contains BOARD SIZE VERT stones), and false otherwise. void update board(int board[][board SIZE VERT], int m, int player num) This function receives as input (a pointer to) the board array, an integer m which is expected to be in the range 1,2,...,BOARD SIZE HORIZ, and an integer player num which should be either 1 or 2. It then updates the board by changing the appropriate entry in the m-th column from 0 to player num. Note that the m-th column of the board is stored in board[m-1][]. Also note that the function must determine which row in the m-th column to update (using the rule that a stone dropped into a given column always slides down to the lowest unoccupied row). int check winner(int board[][board SIZE VERT], int last move) This function receives as input (a pointer to) the board array and an integer last move, which is interpreted as the index of the column (in the range 1,2,...,BOARD SIZE HORIZ) where the most recent stone was played. The function then checks whether the placement of this most recent stone results in a win (four stones on the board forming a consecutive horizontal, vertical, or diagonal sequence). If so, the function returns the player number (either 1 or 2) of the winning player. If there is no winner, the function returns 0. In addition to the eight functions described above, you are welcome to declare and implement other functions, although you are not required to do so. For example, you might wish to break down the function check winner() into three functions that check for a winning sequence in each of the three possible directions: check horizontal(), check vertical(), and check diagonal(). If you do implement additional functions, you should write both the definitions and the forward declarations of these functions in the file connect4 functions.c. Compilation Instructions: Put all the files, namely connect4.c, connect4 functions.h, and connect4 functions.c, in the same directory (also called folder on some systems). If you are compiling with gcc, you should compile only the.c files. Use: gcc -Wall -std=c99 connect4.c connect4 functions.c -o connect4 If you are using an integrated development environment (IDE), simply add all the files to your project. For example, in Pelles C, assuming your project is called connect4.exe, you should right-click on connect4.exe in the source tree on the right side of the IDE. Select the option Add files to project to add both connect4 functions.c and connect4 functions.h to the project. 6

Here is a sample run of the entire program, assuming that the executable file is called connect4. This run illustrates a situation where Alice wins the game. The user input is underlined, as usual. /home/userxyz/ece15/lab4> connect4 *** Welcome to the Connect Four game!!! *** Would you like to make the first move [y/n]: *? Please enter your move: 4 Computer moved in column: 5 X O Please enter your move: 4 7

X O Computer moved in column: 3 O X O Please enter your move:? Please enter your move: *4 Please enter your move: 9 4 Please enter your move: -4 4 Please enter your move: 4-4 O X O 8

Computer moved in column: 3 O X O X O Please enter your move: 4 O X O X O ***************************** * Player X won!!! Game over * ***************************** Notes: Note that in board[m][n], the first number m represents the column whereas the second number n represents the row. It is recommended to identify board[0][0] with the upper-left cell of the board, so that board[6][5] is the lower-right cell. Observe that board[m][n] should contain 1 if player X has a stone in this cell, 2 if player O has a stone in this cell, and 0 otherwise. The return type of the functions check win or tie() and is column full() is bool. This data type was covered in Lecture Unit #3. It is defined in the standard library header file <stdbool.h>, which is included in connect4 functions.h. This header file also defines the constants true and false as 1 and 0, respectively. 9

Extra Credit (up to 100 points) To earn extra credit in this lab, you should implement a winning strategy for the Connect-Four game. The better your strategy, the more extra-credit points you are likely to receive. Specifically, you need to implement the following function: int best move(int board[][board SIZE VERT], int computer num) in the file connect4 functions.c that you submit. A forward declaration for this function is already provided in the file connect4 functions.h. The function should be identical in its functionality to the function random move() described on pp. 4 5, with one important difference: instead of generating a random move, the function should return the best move (that you can come-up with) in each position. Of course, if you would like your best move() function to call other functions, you are welcome to do so: declare and define these additional functions in the file connect4 functions.c. The function best move() that you submit will be, first, tested to verify that it produces valid moves and correctly updates the state of the board (using the update board(board,m,computer num) function call). It will be then tested against the random move() function a 100 times, with the requirement that it wins at least 66 of the 100 games. Another requirement is that you should not use global variables anywhere in connect4 functions.c, since global variables make cause a collision when functions written by different students are jointly compiled together. All the best move() functions that pass these tests will play against each other in a round-robin competition. Since making the first move gives a distinct advantage in the Connect-Four game, each pair of best move() functions A and B will play each other twice: once with A making the first move, and once with B making the first move. The round-robin competition will be scored as in chess: for each game played, a win is 1 point, a draw is 0.5 points for both players, a loss is 0 points. The best move() function that takes first place (or ties for first place) in the round-robin competition will be awarded 100 extra-credit points. The best move() function that places last in the competition will not receive any extra credit. All the other best move() functions will earn extra-credit points prorated linearly according to their rank in the competition. For example, if 21 students take part in the competition and there are no ties, then first place will earn 100 points, second place will earn 95 points, third place 90 points,..., 19-th place 10 points, 20-th place 5 points, and last place 0 points. 10