Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent.

Slides:



Advertisements
Similar presentations
Coordinate Plane 3.8 Coordinate Plane Is also referred to as the …. “Cartesian Plane”
Advertisements

Tic Tac Toe size(600,600); Aim: How can we set up our canvas and display for a Tic Tac Toe game? 1. Sketch the two drawings and write the two code.
Tic Tac Toe Game Design Using OOP
Tic Tac Toe Game playing strategies
CHAPTER 10 FUN AND GAMES Group 1: Xiangling Liu.
TIC-TAC-TOE FELIX CHEN CLUSTER 5: Computers in Biophysics and Robotics.
Microsoft® Small Basic Advanced Games Estimated time to complete this lesson: 1 hour.
PLANNING THE TIC TAC TOE GAME BY NEEL DAVE. TIC TAC TOE INSTRUCTIONS Tic Tac Toe Instructions The basic concept of Tic Tac Toe 1.This is a game for two.
Tutorial 6 of CSCI2110 Bipartite Matching Tutor: Zhou Hong ( 周宏 )
J AVA A SSIGNMENT 1. O VERVIEW Tic Tac Toe How it should work Using the supplied methods What you need to do How we will test your code.
Presented by : Ashin Ara Bithi Roll : 09 Iffat Ara Roll : 22 12th Batch Department of Computer Science & Engineering University of Dhaka.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
CS 61B Data Structures and Programming Methodology July 31, 2008 David Sun.
1 CSE1301 Computer Programming: Lecture 23 Algorithm Design (Part 1)
Copyright Notice A Homemade PowerPoint Game by Sarah Grabowski University of Georgia Let’s Play Directions Story Credits Preparation Visible Game Board.
Unit 4: Linear Relations Unit 4: Linear Relations.
Coordinates and Design.  What numbers would you write on this line?  Each space is 1 unit 0.
Strategy Games for Older Students YUMI DEADLY CENTRE School of Curriculum Enquiries: YuMi Deadly.
Lesson 2-2 Example Solve. Tic Tac Toe Katrina and her friend played Tic Tac Toe. The outcomes of the games are shown in the line plot. Did Katrina.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
1 CSE1301 Computer Programming: Lecture 25 Software Engineering 2.
Excel Battleship.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Today in CS161 Lecture #4 Solving Problems with Computers Walk through the Tic Tac Toe Algorithm Getting ready for Creating Programs Turn the Inches to.
Arrays for Random Storage Another way to use an array is to create an array with dozens, hundreds or thousands of items and then randomly select from them.
Coordinates and Design
Introducing Arrays We will often need to store collections of information –a list of names to sort –a list of values to compute averages, standard deviation,
BattleShips. A popular game. Have you played it before? How to Play Battleship For much of its life, Battleship was a traditional board game played without.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Once the opposing player has scored a hit on all of the space for a particular ship, you must call out, “Hit…you sunk my ___________”. Once a player has.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
Games. Adversaries Consider the process of reasoning when an adversary is trying to defeat our efforts In game playing situations one searches down the.
Code Design Using Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Algorithms Writing instructions in the order they should execute.
+ TIC-TAC-TOE GAME CAPSTONE PROJECT SEN Team Members Sno.NameITU ID 1Keerthi Alimity Venkata Ganugapati Sujitha Sanku Bavi Bharathan87550.
Arrays and Logic Let’s generate 6 lotto numbers using a loop and the random number generator – but we don’t want to duplicate any numbers – how do we know.
Introducing Arrays We will often need to store collections of information –a list of names to sort –a list of values to compute averages, standard deviation,
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Discussion 2: Intro to Project 1: Chess Submit Project 1 under # 11!
Warship C++: An entity of Battleship
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
Battleship Microsoft Excel. Objective & Performance Outcome Students will follow a series of instructions presented by the teacher. You will need to take.
6.2 Classes “ A class is basically a structure with member functions as well as member data. Classes are central to the programming methodology known as.
Programming for Art: Arrays – 2D ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 16 Fall 2010.
Battleships. You need to know How to draw two 11 x 11 grids How to read off names of squares.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
How to play battle ships…
Intro to Computer Science II
BATTLESHIP! BATTLESHIP!
A Homemade PowerPoint Game by Sarah Grabowski University of Georgia
Home Screen Aircraft Carrier A B C D E F G H
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
Lesson Plan Title: Sea Battle (Paper-pencil Game) Level: Middle
A Homemade PowerPoint Game by Sarah Grabowski University of Georgia
BATTLEBEDROOM! DORMITORIO!
Battleship
Duo By: Fernando & Vivian.
Arrays and Logic Let’s generate 6 lotto numbers using a loop and the random number generator but we don’t want to duplicate any numbers how do we know.
A Homemade PowerPoint Game by Sarah Grabowski University of Georgia
A Homemade PowerPoint Game by Sarah Grabowski University of Georgia
Tic Tac Toe application
A Homemade PowerPoint Game by Sarah Grabowski University of Georgia
A Homemade PowerPoint Game by Sarah Grabowski University of Georgia
Tic-Tac-Toe Game Engine
A Homemade PowerPoint Game by Sarah Grabowski University of Georgia
2019 SAIMC Puzzle Challenge General Regulations
Unit II Game Playing.
Presentation transcript:

Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent where players might be on the game board (like our GameBoard example from earlier) – For more complex games, we store all of the values in an array in tic tac toe requires that we know where each X and O have been stored in chess, we need to know in which square every piece currently resides in a card game, we have to store each card dealt to the player So we will use arrays to represent the game board and any special situations

Tic-Tac-Toe We can represent tic-tac-toe as a 3x3 game board of chars (‘X’, ‘O’, ‘ ’ or ‘b’) – char[ ][ ] ttt = new char[3][3]; The user will input their next move as two values, for instance, 2, 2 to represent the middle square, or 1, 3 to represent the middle row, rightmost square – If they are entering values from 1-3,1-3, we have to subtract 1 from each – why? Or we can make them enter values from 0-2 and 0-2 We can also represent tic-tac-toe as a 1D array from 0-8 as shown below – Which is easier?

Tic Tac Toe Logic For this game, you need to do the following – Create the board – Repeat until someone has won or the game is a draw (you can keep track of this by counting turns, if turn = 9, then the board is filled) Draw the board on the screen Get player 1’s guess and data verify that the guess is within bounds (0-2, 0-2, or 1-3, 1-3 if you prefer, or between 0 and 8) and that the square is empty (ttt[row][col]==‘ ’) or (ttt[row][col]==‘b’) if you are using b for blank (empty) square Check to see if player 1 has won the game with this move Otherwise, repeat with player 2 (get pick, data verify, check for win) – Output the winner

Human vs Computer Version If you want to play against the computer, you replace player 2 with the computer generating a move – How does the computer make a pick? Randomly – just generate a number from 0-8 (or two numbers from 0 to 2) Try to win/not to lose – check to see if player 1 can win on next move, if so, block, else check to see if player 2 can win on next move, if so use it, else randomly generate move Strategy – pick center square if available, else pick whichever corner is available, else pick a middle square Combine “try to win” with “strategy” for the best computer version – For a real elaborate program, ask the user to input a skill level (0 for random, 1 for try to win, 2 for strategy, 3 for combined)

How do you check for a win? This depends on whether you have used the 2D or 1D array version – If 2D, you want to compare all 3 squares of a row to see if they are the same and are all Xs or Os Testing row 0: – if(ttt[0][0]==ttt[0][1]&&ttt[0][0]==ttt[0][2]&&(ttt[0][0]==‘X’||ttt[0][0] ==‘O’)) … – Do the same for [1][0], [1][1], [1][2] and [2][0], [2][1], [2] How do you test for a column? – if(ttt[0][0]==ttt[1][0]&&ttt[0][0]==ttt[2][0]&&(ttt[0][0]==‘X’||ttt[0][0] ==‘O’)) … How do you test for the two diagonals? – If you are using the 1D array, its pretty much the same but you have to figure out the indices for each row, column and diagonal Testing column 0: – if(ttt[0]==ttt[3]&&ttt[0]==ttt[6]&&(ttt[0]==‘X’||ttt[0]==‘O’))…

Another Game: Battleship In the game of battleship, two players play on 10x10 grids – Each player places their ships somewhere on the grid – Then, players take guessing where a ship might be by calling out coordinates such as 5, 7 – If the other player has a ship at 5, 7, they announce “hit” else they announce “miss” – If you hit a player’s ship, you try to destroy it by hitting it at all coordinates. For instance, a submarine covers 3 grid positions, perhaps at 5, 6, and 5, 7, and 5, 8 – So if found at 5, 7, you would check 4, 7, and 6, 7 or 5, 6 and 5, 8 and then try another neighboring position until you have destroyed it NOTE: ships must either be placed horizontally or vertically, not diagonally, and ships cannot go off the 10x10 grid or overlap each other Ship sizes vary depending on type: aircraft carriers are 5 grid positions long, battleships 4, submarines and destoyers 3, and patrol boats 2 The player who has destroyed all of the other player’s ships wins the game

Our Battleship Game Since you and another player cannot both look at the screen (you would see each other’s setup), we will implement a Battleship solitaire game – The computer will randomly position 5 ships on the 10x10 grid – have to make sure that no ships overlap and no ships run off the screen (see the logic in two slides) – Iterate until either you have sunk all of the computer’s ships, your number of picks have elapsed, or you get sick of playing and want to quit Get the user’s next pick and increment the number of picks Check to see if that pick is legal (between 0 and 9) Check to see if the pick is a hit (if so, mark it so in the array) or a miss If a hit, see if it sinks a ship (has the ship been hit in every position?)

The Battlefield The battlefield will be a 10x10 array of char’s Each element should store – ‘ ’ if that position has no ship in it and hasn’t been guessed yet – ‘a’-’e’ if that position is part of a boat and hasn’t been guessed yet where the boats are labeled ‘a’ for aircraft carriers, ‘b’ for battleships, ‘c’ for submarines, ‘d’ for destroyers, and ‘e’ for patrol boats – ‘m’ if the user has guessed that position and it is a miss – ‘h’ if the user has guessed that position and it is a hit – ‘s’ if the user has sunk that boat by hitting all parts of it The array is originally declared to be all ‘ ’ and then the computer randomly generates which boats go into which of the slots, so before the game starts, the board is all ‘ ’, ‘a’, ‘b’, …, ‘e’ – While the game is in session, the computer changes squares to store ‘h’, ‘m’ and ‘s’ as needed

Computer Placing Ships This is the trickiest part of the program – We have to make sure that the computer places the 5 boats such that none overlap and none go off the board – One strategy is as follows: First select horizontal or vertical Next, select a starting point between 0 and 10 – shipsize – for instance, a battleship could go from 10 – 4 = 6 onward (6 to 9) in either the horizontal or vertical direction Verify that this does not intersect another ship and if so, try again Another tricky part of the game is determining if a ship has been sunk – You can do this by having five variables, one for each boat type, initialized to 5, 4, 3, 3 and 2 For each hit, decrement the appropriate variable based on the ship type and then if that variable is 0, you have sunk that ship

Storing Graphics Data in an Array Random generator = new Random(); int i, red, green, blue; int[] x, y, size; Color[] color; x = new int[25]; y = new int[25]; size = new int[25]; color = new Color[25]; for(i=0;i<25;i++) { x[i]=generator.nextInt(480) + 10; y[i]=generator.nextInt(480) + 10; size[i]=generator.nextInt(10) + 3; red=generator.nextInt(256); green=generator.nextInt(256); blue=generator.nextInt(256); color[i]=new Color(red, green, blue); } g.setColor(Color.black); g.fillRect(0,0,500,500); for(i=0;i<25;i++) { g.setColor(color[i]); g.fillOval(x[i], y[i], size[i], size[i]); } We use 4 arrays x, y for the coordinate size for the size of the oval color to store the Color of the oval