Tic Tac Toe Game! Day 8

Similar documents
D - Tic Tac Toe. Let's use our 9 sparkles to build a tic tac toe game! 2017 courses.techcamp.org.uk/ Page 1 of 9

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

Lab Activity #7 2D Arrays and File Streams

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

BOREDGAMES Language for Board Games

Functionally Modular. Self-Review Questions

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

2D Array Practice. Lecture 26

CSci 1113 Lab Exercise 6 (Week 7): Arrays & Strings

CSC 108. Prof. Yue-Ling Wong. Lab 6: Tic-tac-toe Game

Lecture 10: for, do, and switch

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

CS159 Midterm #1 Review

Two Dimensional Arrays

Senet. Language Reference Manual. 26 th October Lilia Nikolova Maxim Sigalov Dhruvkumar Motwani Srihari Sridhar Richard Muñoz

Haskell Refresher Informatics 2D

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

Question 1 [20 points]

CS 102 Lab 3 Fall 2012

AP Computer Science Lists The Array type

BEGINNER PHP Table of Contents

Tic-Tac-Toe. By the time you are done with this activity, you and your team should be able to:

1) Log on to the computer using your PU net ID and password.

Session 8.2. Finding Winners Using Arrays

Exercise: Inventing Language

LOOPS. Repetition using the while statement

Lab 9: Pointers and arrays

Blocky: A Game of Falling Blocks

6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

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

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

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

COMP1917: 09 Arrays and Strings

EECS 183. Week 3 - Diana Gage. www-personal.umich.edu/ ~drgage

CS 314 Exam 2 Spring

More Arrays. Last updated 2/6/19

Declaring and ini,alizing 2D arrays

RANDOM NUMBER GAME PROJECT

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

This Week s Agenda (Part A) CS121: Computer Programming I. The Games People Play. Data Types & Structures. The Array in Java.

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Structs. Comp Sci 1570 Introduction to C++ Introduction. Aggregate data. Example. General syntax Object initialization Initialization and access

EECE.2160: ECE Application Programming Fall 2017

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Unit 7. 'while' Loops

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

Noughts and Crosses. Step 1: Drawing the grid. Introduction

GridLang: Grid Based Game Development Language. Programming Language and Translators - Spring 2017 Prof. Stephen Edwards

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

SFU CMPT Topic: Control Statements

Introduction to Computer Programming

Control Structures: The IF statement!

I101/B100 Problem Solving with Computers

Lab 4 Due April 18 th

Learning objec-ves. Declaring and ini-alizing 2D arrays. Prin-ng 2D arrays. Using 2D arrays Decomposi-on of a solu-on into objects and methods

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

More Data Types. The Char Data Type. Variable Declaration. CS200: Computer Science I. Module 14 More Data Types

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

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

//If target was found, then //found == true and a[index] == target.

CSE120 Wi18 Final Review

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

EECE.2160: ECE Application Programming Fall 2017

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

Lab 12: Lijnenspel revisited

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment

Computer Programming

CS 103 Lab - Party Like A Char Star

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

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

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

[ the academy_of_code] Senior Beginners

General Certificate of Education Advanced Subsidiary Examination June 2010

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Informatics 2D. Coursework 1: Search and Games

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

Basic Computer Programming (Processing)

CS Homework 10 p. 1. CS Homework 10

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Collections

CS31 Discussion 1E Spring 17 : week 08

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Lecture #9 **go to Blackboard ** Review from Shackelford Reading If statements Loops. Using if statements and loops

Computer Programming for Engineering Applica4ons. Intro to Programming 10/22/13 1 ECE 175. Mul4- dimensional Arrays

Python Games. Session 1 By Declan Fox

Assignment #1 Simple C++

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

CS1 Studio Project: Connect Four

CS1114 Spring 2015 Test ONE ANSWER KEY. page 1 of 8 pages (counting the cover sheet)

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Introduction to Programming I COS1511 School of Computing Revision Notes

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Study Guide for Test 2

Java Identifiers, Data Types & Variables

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

Ensuring a Rigorous Curriculum: Practices and Goals

CS12020 for CGVG. Practical 2. Jim Finnis

Transcription:

Tic Tac Toe Game! Day 8

Game Description We will be working on an implementation of a Tic-Tac-Toe Game. This is designed as a two-player game. As you get more involved in programming, you might learn how to design one with the computer using random numbers :) For this game, there will be two players one represented as x and one as o. The first turn is Player X s turn. Each player can place one piece on the board during their turn. The player decides where to place their piece by inputting a row and column coordinate. A player wins when 3 of their pieces form a line; diagonal, horizontal, and vertical all count.

Example Run:

Creating the tic-tac-toe board (part 1) In order to create the tic-tac-toe board, we store it as a string. But first let s write the board as a series of strings! The first row of the board as a string would be: string row1 = 1 2 3\n ; index counting: string row2 = 1 \n ; index counting: 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 12 string row3 = - - - - -\n ; index counting: 0 1 2 3 4 5 6 7 8 9 10 11 // what are the last 3 rows? // remember that \n is ONE character

Creating the tic-tac-toe board (part 2) Once you have all the rows stored in strings, we want to put them into one string. Remember from earlier: string str1 = pan ; string str2 = cake ; string str3 = str1 + str2; // what does str3 store? So if we currently have string row1 = 1 2 3\n ; string row2 = 1 \n ; string row3 = - - - - -\n ; etcetera, how would we put them into one string that stores the whole board? string board = ;

printboard This function should take in a string that represents the entire board. Then it should output the string to the console. HINT: do not return the string! For aesthetic purposes, maybe cout newlines before and after the board so it is separated a bit from the other outputs.

String [] The string class has the bracket function. It can take in an index position inside the [] and will return the character at that position. This function is unique because it allows you to change the value of the char inside the string. Ex. // These do the same thing string word = California Adventure ; char letter1 = word.substr(1,1); char letter2 = word[1]; //But, we can edit chars in the string! word[1] = A ; cout << word; //What is the output?

getindex This function should accept two int parameters, a row position and a column position. It should return the index in the giant string board where that coordinate is located at. Ex: getindex(1,1); string row1&2 = 1 2 3\n1 \n ; If I want to change the value in the string, I would have to know the index. Which is why we need this function.

Indexing board: Remember! The entire string board written out would look like: string board = " 1 2 3\n1 \n - - - - -\n2 \n - - - - -\n3 \n"; If we wanted the index of position (1,1), the function getindex would return 14. If we wanted the index of position (1,2), the function getindex would return 18. If we wanted the index of position (1,3), the function getindex would return 22. 1 2 3\n1 \n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

getindex int getindex(int row, int col){ if(row == 1 && col == 1){ return 14; } else if( row == 1 && col == 2){ return 18; } else if( row == 1 && col == 3){ return 22; } // what is the rest of the code? }

Passing by Value Let s create a function that adds ten to the number inputted into the function //Outside of main int add_10(int val){ val += 10; return val; } //Using in main int main(){ int numb = 20; add_10(numb); cout << numb; return 0; } We had explained earlier that a variable passed into the functions would be only take the value of it. This line is taking the value of 10 and saving 10 into val. When this function is returned, this line will have a value of 20. But has numb changed? What if we want numb to change?

Passing by Reference When declaring a function, we can pass in a variable by reference which means it will be able to update changes to the variable passed in a function. If you want to pass in a variable by reference, you will include an & before the name of the variable in the function parameter list. Ex: int add_10(int &val){ } val += 10; return val; Can only use this with variables passed in! int main(){ int numb = 10; add_10(numb); add_10(numb); add_10(numb); add_10(10); //ERROR! DO NOT DO PLz } cout << numb; //what is the output

Visual Representation

mark : Place pieces on board This function will place an x or o in a position, given as row and column, on the board. What information (parameters) do we need to make this function? What data types are the parameters? What would the return type be?

mark So mark needs : Row number where the piece will be placed (int) Column number where the piece will be placed (int) Entire board (string) Which piece it is, either x or o (char) Should any be passed by reference?

win: checking to see if someone won Write a function called win which checks to see if someone won! You decide on which parameters the function should have. We recommend that function returns a bool (true if someone won and false if someone has not won). Remember, there are 8 ways to win tic tac toe!

win General idea: we want to see if there are three of the same mark in a row. Example: //checking to see if there are three x s in the first row if (board[14] == x && board[18] == x && board[22] == x ) { return true; } //what are the other 7 ways for x to win? What about o winning? Note: we must use the single quotes when writing x because the viewing brackets look at a char within the string at a particular index.

Optional Functions printboard, getindex, mark, and win are the four functions that are necessary for the tic tac toe game to work. However, you can also write other functions that may be helpful in your code! One suggestion is making a winner function, which will cout a message depending on who won (or if no one won). If you have any other function in mind that you want to code, let one of us know and we can help you construct it!

Writing the tic tac toe program Most games that we know are repetitive in some way or another. Tic Tac Toe is not any different. Therefore, it would be a good idea to use a loop somewhere in our game. Think about it... What parts of the game are repeated? What is the most number of times this process is repeated? What is the least number of times this process is repeated? When should the repeated process stop? (What are the conditions?) What would be the best loop to use?

Alternating Turns Within the loop, Player X and Player O alternate turns. This means that: if (it is Player X s turn), certain code should be executed, or else it is Player O x turn and certain code should be executed. There are several different ways that we can write the conditions for this code. Please share with us your ideas on how we could do this!

Player X s Turn First the computer will tell them it s their turn as well as asking them where would they like to move. Then the player must enter the row and column of where they would like to move. The numbers should be separated by a space. Lastly the board is printed with their move. How would we code this? Remember! Use your functions.

Player O s Turn Player O s turn is very similar to Player X s turn! First the computer will tell them it s their turn as well as asking them where would they like to move. Then the player must enter the row and column of where they would like to move. The numbers should be separated by a space. Lastly the board is printed with their move. How would we code this? Remember! Use your functions.

Concluding the game After the game, if you want to print a message based off of who won, or a tie, this code or (function) would be used after the loop! If you do not wish to include this part, that s fine. Congratulations! You have created a tic-tac-toe game! We thank you for sticking around the entire two weeks (: