CS 210 Fundamentals of Programming I Spring 2015 Programming Assignment 4

Similar documents
Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday)

CS 215 Fundamentals of Programming II Spring 2011 Project 2

CS 215 Fundamentals of Programming II Spring 2018 Project 5

CS 215 Fundamentals of Programming II Fall 2017 Project 7. Morse Code. 30 points. Out: November 20, 2017 Due: December 4, 2017 (Monday) a n m

CS 215 Fundamentals of Programming II Spring 2010 Project 6. Dnode<T> structure. Specifications for Document Class. 30 points

CS 101, Spring 2014 April 1st Exam 2 Question 1. [3 points] What output is printed by the following code?

Write code that simulates one roll of a 6-sided die and prints out the

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

EECE.2160: ECE Application Programming

CPSC 217 Assignment 3

CS 3204 Operating Systems Programming Project #2 Job / CPU Scheduling Dr. Sallie Henry Spring 2001 Due on February 27, 2001.

CS 101, Spring 2016 March 22nd Exam 2

15-110: Principles of Computing, Spring 2018

2005 Stanford Local Programming Contest

EECE.2160: ECE Application Programming Spring 2019

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

Question 2. [2 points] True False By default, structures are passed-by-reference.

Tips from the experts: How to waste a lot of time on this assignment

Tips from the experts: How to waste a lot of time on this assignment

CS 2604 Minor Project 1 DRAFT Fall 2000

CS 1803 Pair Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 29th, before 6 PM Out of 100 points

CS 470 Operating Systems Spring 2013 Shell Project

Due Friday, March 20 at 11:59 p.m. Write and submit one Java program, Sequence.java, as described on the next page.

cd h: mkdir -p CS101 cd CS101 curl -O unzip zipfile cd CS101_Exam4

CS 2110 Summer 2011: Assignment 2 Boggle

EECE.2160: ECE Application Programming Spring 2017

CS 142 Style Guide Grading and Details

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Other Loop Options EXAMPLE

Assignment 3 ITCS-6010/8010: Cloud Computing for Data Analysis

FALL 2017 CSCI 304 LAB1 (Due on Sep-19, 11:59:59pm)

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

Question 1. [5 points] Circle and briefly explain the error(s) in the following code:

CS 2316 Pair 1: Homework 3 Enigma Fun Due: Wednesday, February 1st, before 11:55 PM Out of 100 points

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

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

Late Penalty: Late assignments will not be accepted.

Programming Assignment 2 ( 100 Points )

15-110: Principles of Computing, Spring 2018

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

Tips from the experts: How to waste a lot of time on this assignment

CS 2604 Minor Project 1 Summer 2000

CS52 - Assignment 8. Due Friday 4/15 at 5:00pm.

CSE 143: Computer Programming II Summer 2015 HW6: 20 Questions (due Thursday, August 13, :30pm)

Assignment 5: MyString COP3330 Fall 2017

How to: Revised 8/22/2016 Copyright 2016 by Region 7 Education Service Center. All rights reserved. Page 1

Project 1: Empirical Analysis of Algorithms

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

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

EECE.2160: ECE Application Programming

ECE2049 Embedded Computing in Engineering Design. Lab #0 Introduction to the MSP430F5529 Launchpad-based Lab Board and Code Composer Studio

Major Assignment: Pacman Game

CS 1803 Individual Homework 2 Conditionals & Loops Due: Wednesday, February 2 nd, before 6 PM Out of 100 points

Pencil-and-Paper Exercises (15 points total)

CMPSCI 187 / Spring 2015 Hangman

Project 1: How to Make One Dollar

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

printf( Please enter another number: ); scanf( %d, &num2);

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

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

Question 1. [5 points] Consider the following partially-complete program, which begins on the left and continues on the right: HERE

Blocky: A Game of Falling Blocks

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2011

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

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

CS 1301 Homework Robot Web Writer

The State Universtiy of New York, Korea Computer Science

Chomp the Graph. Research. treet. road. Sam Magura, Vitchyr Pong, Elliot Cartee, Kevin Valakuzhy. Scientific

Read this before starting!

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

: Principles of Imperative Computation. Fall Assignment 5: Interfaces, Backtracking Search, Hash Tables

CPSC 217 Assignment 4

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

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

CSE 504: Compiler Design

PIC 10B Lecture 1 Winter 2014 Homework Assignment #2

CS 101, Spring 2017 Apr 11th Exam 3

PA3: Violet's Vending Venture (Version 3.0)

EECE.2160: ECE Application Programming Spring 2018

Be sure check the official clarification thread for corrections or updates to this document or to the distributed code.

CS 2316 Individual Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 18th, before 11:55 PM Out of 100 points

CS106A Handout 18 Winter February 3, 2014 Practice Midterm Exam

2 Steps Building the Coding and Testing Infrastructure

Lesson 10: Quiz #1 and Getting User Input (W03D2)

1. Introduction. 2. Deliverables

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

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

EECE.2160: ECE Application Programming Spring 2018 Programming Assignment #1: A Simple C Program Due Monday, 1/29/18, 11:59:59 PM

Homework Assignment 2: Java Console and Graphics

CS 375 UNIX System Programming Spring 2014 Syllabus

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

Read this before starting!

Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018

Question 2. [2 points] Which of the following is a correct statement to obtain user input? (Assume that fleems is an int variable.

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

Data Structure and Algorithm Homework #3 Due: 1:20pm, Thursday, May 16, 2017 TA === Homework submission instructions ===

CS 1301 Pair Homework 4 Conditionals & Loops Due: Friday, October 1, before 6 PM Out of 100 points

Transcription:

CS 210 Fundamentals of Programming I Spring 2015 Programming Assignment 4 20 points Out: February 18/19, 2015 Due: February 25/26, 2015 Reminder: This is a programming assignment, and work on this assignment should be done individually. Assistance from other students is limited to questions about specific issues as noted in the syllabus. Problem Statement The game of Nim has existed in various forms since at least the 16th century (according to Wikipedia, so it must be true ). It is fairly simple, yet still provides a challenge. Here are the rules for our version: 1. The game "board" consists of three heaps of stones initially containing 3, 4, and 5 stones each. 2. Players alternate turns removing stones from a single heap each turn. 3. A player may remove as many stones as they wish in a single move, but they must all come from the same heap. 4. The player that removes the last stone wins. Our program will labels the heaps as 'A', 'B', and 'C'. A move will be entered as the heap letter followed by the number of stones to be removed. For a move to be legal, the letter must be 'A', 'B', or 'C', and the number that follows must be greater than 0 and less than or equal to the number of stones remaining in that heap. See the sample run for some illustrated games. Like last time, we want to create a program to allow a human to play this game against a computer opponent. This time the computer opponent will use a perfect strategy. (That is, if it goes first, it will always win. If it goes second, and the human makes a mistake, it will win.) If you want to know more, the Wikipedia article provides more information than you will ever need. The program should allow the player to play as many games as she likes. For each new game, the loser of the previous game goes first. Therefore, if both players play perfectly, the games will be split evenly. Program Specifications Like the last two assignments, you are given the program specification in the form of an analysis for a series of functions that must be implemented. Unlike the previous assignments, only design hints are given rather than complete designs. Each function accomplishes one task needed for this program. You should write a function and then test it with a main program that ensures that the function works by itself. Once the function is working, move on to the next function, which will require a different main function to test it. Once all of the individual functions work, the final main function that actually will play the game is written. Function: print_greeting No objects Design: This function's task is simply to print out the greeting at the beginning of the program. See the sample run for the exact text format. It should only be run once, when the user first runs the program, not at the start of each game. 02/17/2015 Page 1 of 6 D. Hwang

Function: user_wishes_to_continue Analysis, Design, and Implementation covered previously. Function: print_scoreboard Objects Type Movement Name number of computer wins int received comp_wins number of user wins int received user_wins Design: This function's task is to print out the current number of wins by the computer and by the user. See the sample run for the exact text format. Function: play_nim Objects Type Movement Name Number of the player that starts int received starting_player Number of the player that won int returned winner Design: This function's task is to play one game of Nim with starting_player going first. The design of this function is left up to you. However, here are some hints: You will need three local integer variables to hold the count for each of the three heaps throughout the game. You will need local variables to hold both the letter of the heap being chosen and the number of stones being removed. You will need a variable that tracks which player s turn it is. You will need to only allow legal moves by the user. A move is legal if: The heap chosen is 'a', 'b', or 'c' (upper or lowercase) The number of stones chosen to be remove is greater than zero and less than or equal to the number of stones in the chosen heap. Note that scanf can be used to read in more than one input by giving a specifier for each input, so to read in a move, use scanf (" %c%d", &chosen_heap, &num_to_remove); If it is the computer s turn, you must call get_computer_move (see next function) to find the move that the computer has chosen. NOTE: While writing this function, you might bypass this step and simply allow the human to enter moves for both players in order to simplify debugging. The function loops until all of the heaps are empty. When this occurs, the player who just moved (i.e., the one who removed the last stone) is the winner and the number of that player is returned to the calling function. Function: get_computer_move Objects Type Movement Name Three heaps of stones int received heap1, heap2, heap3 Computer's chosen heap char passed back chosen_heap Computer's chosen number of stones int passed back number_to_remove 02/17/2015 Page 2 of 6 D. Hwang

Design: The winning strategy for this game relies on counting and canceling out powers of two. Again, read the Wikipedia article if you are interested in the details. The short answer is that we can find what is called the Nimnumber of the game by computing the bitwise exclusive or (XOR) of all the heaps. Fortunately, C has an operator that can do this (^) (not exponentiation). If we can make the Nim number of the game be 0 after every move, we are guaranteed to win. Here is the algorithm: 1. Calculate the Nim number of the game by XOR ing all the heaps together. (e.g. nim_number = heap_a ^ heap_b ^ heap_c; ) 2. If that number is already 0, we do not have a good move, so simply remove 1 stone from the first heap that has one available. (We are going to try to stall for time and hope the user makes a mistake). 3. Otherwise, we need to find a move that changes the Nim number to 0. To do this we perform the following until we find a heap that works: (a) If (heap ^ nim_number) < heap, then this is the heap that we will remove stones from. (b) The number of stones to remove will be heap - (heap ^ nim_number) Main Program Objects Type Name Number of wins by the computer int comp_wins Number of wins by the user int user_wins Starting player number (initially the user) int starting_player Design: The main function for this program ends up being fairly simple and similar to the last assignment. Most of the complicated logic is in the other functions. Main needs to keep track of the number of wins for each player and which player is the starting player for the next game. It needs to repeatedly call the play_nim function, record the winner, print out the current scoreboard, and ask the user if they wish to continue (by calling that function) until the user is done playing. Assignment Write a C program that implements the game of Nim. Your program must follow the specifications given above to earn full credit. That is, your program must define and use at least the specified functions. (The names of functions and variables do not have to be exactly the same.) The output of the program must conform exactly to the following example run (now that we have loops, there is only one sample run; user input shown in bold). Note there is blank line before each player's move, before the scoreboard display, and before the "Do you wish to continue?" prompt, and there is one space after the first sentence punctuation in a two sentence line of output. (E.g. in the error messages.) And as usual, there must be a newline after the last line of output. Welcome to the Ancient Game of Nim is you (the human) is me (the computer) goes first this time! 02/17/2015 Page 3 of 6 D. Hwang

A: 5 Enter the letter of the heap and number of stones to remove: c2 A: 5 A: 4 Enter the letter of the heap and number of stones to remove: B2 A: 4 Enter the letter of the heap and number of stones to remove: D4 Illegal move! Try again. Enter the letter of the heap and number of stones to remove: c2 Illegal move! Try again. Enter the letter of the heap and number of stones to remove: c1 02/17/2015 Page 4 of 6 D. Hwang

A: 2 Enter the letter of the heap and number of stones to remove: b1 A: 2 B: 1 A: 1 B: 1 Enter the letter of the heap and number of stones to remove: b1 A: 1 B: 0 wins! * * * * * * * * * * * Current Score: (Human): 0 (Computer): 1 Do you wish to play again? (y/n) Y goes first this time! A: 5 Enter the letter of the heap and number of stones to remove: a5 02/17/2015 Page 5 of 6 D. Hwang

A: 0 COMPUTER moves b1 A: 0 B: 3 Enter the letter of the heap and number of stones to remove: b3 A: 0 B: 0 COMPUTER moves c3 wins! * * * * * * * * * * * Current Score: (Human): 0 (Computer): 2 Do you wish to play again? (y/n) p Bad input! Try again. Do you wish to play again? (y/n) n REMINDER: Your program must compile for it to be graded. Submissions that do not compile will be returned for resubmission and assessed a late penalty. Submissions that do not substantially work also will be returned for resubmission and assessed a late penalty. Follow the program documentation guidelines in the C Programming Style Guideline handout. As stated in the syllabus, part of the grade on a programming assignment depends on how well you adhere to the guidelines. The grader will look at your code and grade it according to the guidelines. Be sure to run the Source code formatter (Astyle) plugin before you submit your program. What to Submit Electronically submit a zipfile containing main.c (only) as explained in class and in the handout Submission Instructions for CS 210. The submission system will start accepting assignments no earlier than the evening of Friday, February 20. Reminders: you may submit as many times as needed, and only the last submission will be graded. All assignments are due by 4:30pm. 02/17/2015 Page 6 of 6 D. Hwang