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

Similar documents
Tic Tac Toe Game! Day 8

2D Array Practice. Lecture 26

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

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

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

Two Dimensional Arrays

CSC 108 Fall hr Test #2 (Nov. 16 (Fri)) Expectations

Lecture 10: for, do, and switch

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

Computer Programming

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

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Functionally Modular. Self-Review Questions

Controlled Assessment Task. Question 1 - Describe how this HTML code produces the form displayed in the browser.

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

CSC 355 PROJECT 4 NETWORKED TIC TAC TOE WITH WPF INTERFACE

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

Digital Media. Seasons Assignment. 1. Copy and open the file seasonsbegin.fla from the Read folder.

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object

Question 1 [20 points]

CS 314 Exam 2 Spring

Lab Activity #7 2D Arrays and File Streams

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

CS 1510: Intro to Computing - Fall 2017 Assignment 8: Tracking the Greats of the NBA

Declaring and ini,alizing 2D arrays

Rock-Paper-Scissors Multiple versions Nested If / Else If / Else Random Numbers

Valuable points from Lesson 6 Adobe Flash CS5 Professional Classroom in a Book

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

COMP519 Web Programming Lecture 16: JavaScript (Part 7) Handouts

Responding to Events. In this chapter, you ll learn how to write code that executes in response. Understanding Event Types 65

CSC 415/515 PROJECT 3 JAVASCRIPT CONCENTRATION GAME. 1. Introduction

ios Tic Tac Toe Game John Robinson at Rowan University

EMP105A-D - How to plan and build a basic form in Front Page.

Task 1. Set up Coursework/Examination Weights

Repetition Structures

15-110: Principles of Computing, Spring 2018

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

In this lab, you will learn more about selection statements. You will get familiar to

Second assignment came out Monday evening. Find defects in Hnefetafl rules written by your classmates. Topic: Code Inspection and Testing

Lecture 3: Truth Tables and Logic Gates. Instructor: Joelle Pineau Class web page:

Excel Skill Module. EPG Workshop. by T. A. Ebert M.E. Rogers

CISC 1600, Lab 3.1: Processing

Programming Project 1

Computer and Programming: Lab 1

(Python) Chapter 3: Repetition

COMP : Practical 8 ActionScript II: The If statement and Variables

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

Provided by - Microsoft Placement Paper Technical 2012

Chapter Four: Loops II

Barchard Introduction to SPSS Marks

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

Project 2: After Image

Functions in ActionScript

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

Ensuring a Rigorous Curriculum: Practices and Goals

mith College Computer Science Week 8 CSC111 - Spring 2015 Dominique Thiébaut

Questions, Projects, and Labs

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

CompSci 94 Making Decisions February 8, Prof. Susan Rodger

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

CSC101 - BMCC - Spring /22/2019. Lab 07

OPEN-SANKORE MANUAL. (Start) button and type Open-Sankoré in the search box, and then click on the Open Sankoré icon as displayed in

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

Why Functional Programming Matters

BOREDGAMES Language for Board Games

IF & VLOOKUP Function

Objective: Class Activities

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Lab 4 Due April 18 th

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

Tips for working efficiently with Excel's fill handle

Duplicate Detection addon for Dynamics CRM by Cowia

Computer Science Lab Exercise 1

Now find the button component in the tool box. [if toolbox isn't present click VIEW on the top and click toolbox]

Haskell Refresher Informatics 2D

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

Informatics 2D. Coursework 1: Search and Games

Barchard Introduction to SPSS Marks

6.001 Notes: Section 15.1

Space Shooter - Movie Clip and Movement

Keypad Quiz Version 5 Quick Start Guide

Decision Making in C

I101/B100 Problem Solving with Computers

Flash Tutorial: Creating Listeners Dynamically

The KWordQuiz Handbook. Peter Hedlund

The Stack, Free Store, and Global Namespace

Com S 227 Spring 2018 Assignment points Due Date: Thursday, September 27, 11:59 pm (midnight) "Late" deadline: Friday, September 28, 11:59 pm

CSC148, Lab #4. General rules. Overview. Tracing recursion. Greatest Common Denominator GCD

We ve covered enough material so far that we can write very sophisticated programs. Let s cover a few more examples that use arrays.

CS3: Introduction to Symbolic Programming. Lecture 10: Tic-tac-toe Lambda.

Programming assignment A

1 Getting started with Processing

Lab 1 Implementing a Simon Says Game

Flash CS4 - Lab 3 Introduction to Classes:

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

SPSS 11.5 for Windows Assignment 2

COSC 3P97 Assignment 1

Project 2 Prototype Report Template Creating Instances Dynamically Dynamic Event Handlers Array Methods 2-D Array; multi-d Arrays Game - Match Them

(Refer Slide Time: 01:40)

Critical Thinking Assignment #6: Java Program #6 of 6 (70 Points)

Transcription:

CSC 108 Spring 2010 Prof Yue-Ling Wong Lab 6: Tic-tac-toe Game 1

What you have for Lab06 so far two 2-D arrays to model the tic-tac-toe game board: board: to store the movieclip instance names of the cells markings: to store the markings by the two players on the board clicking on a cell will: show the marking based on the currplayer update the corresponding value in the markings array switch player after clicking on a cell a text giving feedback on whose turn it is 2

The Array markings Beginning of the game markings[0][0] markings[0][1] markings[0][2] 0 0 0 markings[1][0] markings[1][1] markings[1][2] 0 0 0 markings[2][0] markings[2][1] markings[2][2] 0 0 0 3

The Array markings After the game has started markings[0][0] markings[0][1] markings[0][2] 1 1 2 markings[1][0] markings[1][1] markings[1][2] 2 0 0 X X O O markings[2][0] markings[2][1] markings[2][2] 0 0 0 4

Tasks to remaining to do 1 reduce code duplication of the current code 2 make the marked cell non-clickable 3 check winner 4 announce who's the winner when there is one 5 take care of things when game over prevent players from clicking on any of the unmarked cells 5

Let's go over code answer of the pre-lab See handout 6

What you should have in the pre-lab two 2-D arrays to model the tic-tac-toe game board: board: to store the movieclip instance names of the cells markings: to store the markings by the two players on the board clicking on a cell will: show the marking based on the currplayer update the corresponding value in the markings array switch player after clicking on a cell a text giving feedback on whose turn it is 7

Step 1 Reduce code duplication 8

board[0][0]addeventlistener(mouseeventmouse_up, makemove00); board[0][1]addeventlistener(mouseeventmouse_up, makemove01); board[0][2]addeventlistener(mouseeventmouse_up, makemove02); board[1][0]addeventlistener(mouseeventmouse_up, makemove10); board[1][1]addeventlistener(mouseeventmouse ] _ UP, makemove11); board[1][2]addeventlistener(mouseeventmouse_up, makemove12); board[2][0]addeventlistener(mouseeventmouse_up, makemove20); board[2][1]addeventlistener(mouseeventmouse_up, makemove21); board[2][2]addeventlistener(mouseeventmouse_up, makemove22); function makemove00(evt:mouseevent):void board[0][0]gotoandstop("player" ] p + currplayer); markings[0][0] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; function makemove01(evt:mouseevent):void board[0][1]gotoandstop("player" + currplayer); markings[0][1] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; function makemove02(evt:mouseevent):void board[0][2]gotoandstop("player" + currplayer); markings[0][2] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; function makemove10(evt:mouseevent):void board[1][0]gotoandstop("player" + currplayer); markings[1][0] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; Problem: Lots of code duplication! We have 9 sets of event listener code 9

What we are aiming at reducing the duplicated d code Aim to have one single event handler function instead of 9 different ones board[0][0]addeventlistener(mouseeventmouse_up, makemove); board[0][1]addeventlistener(mouseeventmouse_up, UP, makemove); board[0][2]addeventlistener(mouseeventmouse_up, makemove); board[1][0]addeventlistener(mouseeventmouse_up, makemove); board[1][1]addeventlistener(mouseeventmouse_up, makemove); board[1][2]addeventlistener(mouseeventmouse_up, makemove); board[2][0]addeventlistener(mouseeventmouse_up, dde t E t MOUSE UP makemove); ) board[2][1]addeventlistener(mouseeventmouse_up, makemove); board[2][2]addeventlistener(mouseeventmouse_up, makemove); function makemove(evt:mouseevent):void 10

board[0][0]addeventlistener(mouseeventmouse_up, makemove00); board[0][1]addeventlistener(mouseeventmouse_up, makemove01); board[0][2]addeventlistener(mouseeventmouse_up, makemove02); board[1][0]addeventlistener(mouseeventmouse_up, makemove10); board[1][1]addeventlistener(mouseeventmouse ] _ UP, makemove11); board[1][2]addeventlistener(mouseeventmouse_up, makemove12); board[2][0]addeventlistener(mouseeventmouse_up, makemove20); board[2][1]addeventlistener(mouseeventmouse_up, makemove21); board[2][2]addeventlistener(mouseeventmouse_up, makemove22); function makemove00(evt:mouseevent):void board[0][0]gotoandstop("player" ] p + currplayer); markings[0][0] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; function makemove01(evt:mouseevent):void board[0][1]gotoandstop("player" + currplayer); markings[0][1] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; function makemove02(evt:mouseevent):void board[0][2]gotoandstop("player" + currplayer); markings[0][2] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; function makemove10(evt:mouseevent):void board[1][0]gotoandstop("player" + currplayer); markings[1][0] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; Problem: Lots of code duplication! Solution: Organize code using function 11

Reduce code duplication using Base Steps: functions 1 identify the duplicated code 2 put them into a new function definition iti 3 replace the original duplicated code with a function call to this function 12

board[0][0]addeventlistener(mouseeventmouse_up, makemove00); board[0][1]addeventlistener(mouseeventmouse_up, makemove01); board[0][2]addeventlistener(mouseeventmouse_up, dd makemove02); board[1][0]addeventlistener(mouseeventmouse_up, makemove10); board[1][1]addeventlistener(mouseeventmouse_up, makemove11); board[1][2]addeventlistener(mouseeventmouse_up, makemove12); board[2][0]addeventlistener(mouseeventmouse_up, makemove20); board[2][1]addeventlistener(mouseeventmouse_up, addeventlistener(mouseevent MOUSE UP makemove21); board[2][2]addeventlistener(mouseeventmouse_up, makemove22); function makemove00(evt:mouseevent):void board[0][0]gotoandstop("player" + currplayer); markings[0][0] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; function makemove01(evt:mouseevent):void board[0][1]gotoandstop("player" + currplayer); markings[0][1] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; See any duplicated statements? t t 13

board[0][0]addeventlistener(mouseeventmouse_up, makemove00); board[0][1]addeventlistener(mouseeventmouse_up, makemove01); board[0][2]addeventlistener(mouseeventmouse_up, dd makemove02); board[1][0]addeventlistener(mouseeventmouse_up, makemove10); board[1][1]addeventlistener(mouseeventmouse_up, makemove11); board[1][2]addeventlistener(mouseeventmouse_up, makemove12); board[2][0]addeventlistener(mouseeventmouse_up, makemove20); board[2][1]addeventlistener(mouseeventmouse_up, addeventlistener(mouseevent MOUSE UP makemove21); board[2][2]addeventlistener(mouseeventmouse_up, makemove22); function makemove00(evt:mouseevent):void board[0][0]gotoandstop("player" + currplayer); markings[0][0] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; function makemove01(evt:mouseevent):void board[0][1]gotoandstop("player" + currplayer); markings[0][1] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; See any duplicated statements? t t 14

board[0][0]addeventlistener(mouseeventmouse_up, makemove00); board[0][1]addeventlistener(mouseeventmouse_up, makemove01); board[0][2]addeventlistener(mouseeventmouse_up, dd makemove02); board[1][0]addeventlistener(mouseeventmouse_up, makemove10); board[1][1]addeventlistener(mouseeventmouse_up, makemove11); board[1][2]addeventlistener(mouseeventmouse_up, makemove12); board[2][0]addeventlistener(mouseeventmouse_up, makemove20); board[2][1]addeventlistener(mouseeventmouse_up, addeventlistener(mouseevent MOUSE UP makemove21); board[2][2]addeventlistener(mouseeventmouse_up, makemove22); function makemove00(evt:mouseevent):void board[0][0]gotoandstop("player" + currplayer); markings[0][0] = currplayer; updategame(); function makemove01(evt:mouseevent):void board[0][1]gotoandstop("player" dst " + currplayer); markings[0][1] = currplayer; updategame(); We could have something like this function updategame():void trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; 15

board[0][0]addeventlistener(mouseeventmouse_up, makemove00); board[0][1]addeventlistener(mouseeventmouse_up, makemove01); board[0][2]addeventlistener(mouseeventmouse_up, dd makemove02); board[1][0]addeventlistener(mouseeventmouse_up, makemove10); board[1][1]addeventlistener(mouseeventmouse_up, makemove11); board[1][2]addeventlistener(mouseeventmouse_up, makemove12); board[2][0]addeventlistener(mouseeventmouse_up, makemove20); board[2][1]addeventlistener(mouseeventmouse_up, addeventlistener(mouseevent MOUSE UP makemove21); board[2][2]addeventlistener(mouseeventmouse_up, makemove22); function makemove00(evt:mouseevent):void board[0][0]gotoandstop("player" + currplayer); markings[0][0] = currplayer; updategame(); function makemove01(evt:mouseevent):void board[0][1]gotoandstop("player" dst " + currplayer); markings[0][1] = currplayer; updategame(); We could have something like this But we want to compact it more function updategame():void trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; 16

board[0][0]addeventlistener(mouseeventmouse_up, makemove); board[0][1]addeventlistener(mouseeventmouse_up, makemove); board[0][2]addeventlistener(mouseeventmouse_up, makemove); board[1][0]addeventlistener(mouseeventmouse_up, dde t E t MOUSE UP makemove); ) board[1][1]addeventlistener(mouseeventmouse_up, makemove); board[1][2]addeventlistener(mouseeventmouse_up, makemove); board[2][0]addeventlistener(mouseeventmouse_up, makemove); board[2][1]addeventlistener(mouseeventmouse_up, makemove); board[2][2]addeventlistener(mouseeventmouse_up, makemove); function makemove(evt:mouseevent):void board[?][?]gotoandstop("player" + currplayer); markings[?][?] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; Won't it be nice to compact titt to like this just ONE event handler function for all 9 cells? 17

board[0][0]addeventlistener(mouseeventmouse_up, makemove); board[0][1]addeventlistener(mouseeventmouse_up, makemove); board[0][2]addeventlistener(mouseeventmouse_up, makemove); board[1][0]addeventlistener(mouseeventmouse_up, dde t E t MOUSE UP makemove); ) board[1][1]addeventlistener(mouseeventmouse_up, makemove); board[1][2]addeventlistener(mouseeventmouse_up, makemove); board[2][0]addeventlistener(mouseeventmouse_up, makemove); board[2][1]addeventlistener(mouseeventmouse_up, makemove); board[2][2]addeventlistener(mouseeventmouse_up, makemove); function makemove(evt:mouseevent):void board[?][?]gotoandstop("player" + currplayer); markings[?][?] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; evtcurrenttarget 18

board[0][0]addeventlistener(mouseeventmouse_up, makemove); board[0][1]addeventlistener(mouseeventmouse_up, makemove); board[0][2]addeventlistener(mouseeventmouse_up, makemove); board[1][0]addeventlistener(mouseeventmouse_up, dde t E t MOUSE UP makemove); ) board[1][1]addeventlistener(mouseeventmouse_up, makemove); board[1][2]addeventlistener(mouseeventmouse_up, makemove); board[2][0]addeventlistener(mouseeventmouse_up, makemove); board[2][1]addeventlistener(mouseeventmouse_up, makemove); board[2][2]addeventlistener(mouseeventmouse_up, makemove); function makemove(evt:mouseevent):void evtcurrenttargetgotoandstop("player" + currplayer); markings[?][?] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; 19

board[0][0]addeventlistener(mouseeventmouse_up, makemove); board[0][1]addeventlistener(mouseeventmouse_up, makemove); board[0][2]addeventlistener(mouseeventmouse_up, makemove); board[1][0]addeventlistener(mouseeventmouse_up, dde t E t MOUSE UP makemove); ) board[1][1]addeventlistener(mouseeventmouse_up, makemove); board[1][2]addeventlistener(mouseeventmouse_up, makemove); board[2][0]addeventlistener(mouseeventmouse_up, makemove); board[2][1]addeventlistener(mouseeventmouse_up, makemove); board[2][2]addeventlistener(mouseeventmouse_up, makemove); function makemove(evt:mouseevent):void evtcurrenttargetgotoandstop("player" + currplayer); markings[?][?] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; What about this one? 20

Basic idea find the position (the two indexes) of currenttarget in the board array use the two indexes in markings[?][?] 21

Recall the gradebook example 22

var gradebook:array = new Array( new Array(82, 91, 73), new Array(64, 76, 55), new Array(78, 95, 68), new Array(87, 72, 92) ); for (var row:int = 0; row < gradebooklength; row++) for (var col:int = 0; col < gradebook[row]length; col++) gradebook[row][col] += 5; stu 0 stu 1 stu 2 stu 3 [0] [1] [2] [3] lab0 lab1 lab2 [0] [1] [2] 82 91 73 64 76 55 78 95 68 87 72 92 23

OK, now return to out tic-tactoe code 24

var gradebook:array = new Array( new Array(82, 91, 73), new Array(64, 76, 55), new Array(78, 95, 68), new Array(87, 72, 92) ); for (var row:int = 0; row < boardlength; row++) for (var col:int = 0; col < board[row]length; col++) 25

var gradebook:array = new Array( new Array(82, 91, 73), new Array(64, 76, 55), new Array(78, 95, 68), new Array(87, 72, 92) ); for (var row:int = 0; row < boardlength; row++) for (var col:int = 0; col < board[row]length; col++) if (evtcurrenttarget == board[row][col]) markings[row][col] = currplayer; break; board[0][0] board[0][1] board[0][2] markings[0][0] markings[0][1] markings[0][2] 1 1 2 board[1][0] board[1][1] board[1][2] markings[1][0] markings[1][1] markings[1][2] 2 board[2][0] board[2][1] board[2][2] markings[2][0] markings[2][1] markings[2][2] 26

board[0][0]addeventlistener(mouseeventmouse_up, makemove); board[0][1]addeventlistener(mouseeventmouse_up, makemove); board[0][2]addeventlistener(mouseeventmouse_up, makemove); board[1][0]addeventlistener(mouseeventmouse_up, dde t E t MOUSE UP makemove); ) board[1][1]addeventlistener(mouseeventmouse_up, makemove); board[1][2]addeventlistener(mouseeventmouse_up, makemove); board[2][0]addeventlistener(mouseeventmouse_up, makemove); board[2][1]addeventlistener(mouseeventmouse_up, makemove); board[2][2]addeventlistener(mouseeventmouse_up, makemove); function makemove(evt:mouseevent):void evtcurrenttargetgotoandstop("player" + currplayer); for (var row:int = 0; row < boardlength; row++) for (var col:int = 0; col < board[row]length; col++) if (evtcurrenttarget == board[row][col]) markings[row][col] = currplayer; break; markings[?][?] = currplayer; trace(markings); if (currplayer == 1) currplayer = 2; else currplayer = 1; txt_feedbacktext = "Player " + currplayer + "'s turn"; 27

Step 2 Make the marked cell nonclickable 28

removeeventlistener() Syntax: targetremoveeventlistener(eventtype, handlername); Example: board[0][0]removeeventlistener(mouseeventmouse_up, makemove); When should you remove the mouse event listener? 29

function makemove(evt:mouseevent):void???removeeventlistener(???, t???); The??? in the code should be identical to what's in the addeventlistener statement of whatever event you want to remove (or disable) 30

Let's look at the second handout 31

Step 3 Check Winner

For Player 1 to Win && && 1 1 1 && && 1 1 1 && && 1 1 1 1 1 && && && 1 && 1 1 && 1 1 && && 1 1 && 1 1 && 1 1 1 1 &&

For Player 2 to Win && && 2 2 2 && && 2 2 2 && && 2 2 2 2 2 && && && 2 && 2 2 && 2 2 && && 2 2 && 2 2 && 2 2 2 2 &&

Separate them into multiple if/else-if statements

function hasawinner():void if (all the cells in the row0 is 1) statement(s); else if (all the cells in the row1 is 1) statement(s); else if (all the cells in the row2 is 1) statement(s); else if (all the cells in the column0 is 1) statement(s); else if (all the cells in the column1 is 1) statement(s); else if (all the cells in the column2 is 1) statement(s); else if (all the cells in the diagonal0 is 1) statement(s); t t( else if (all the cells in the diagonal1 is 1) statement(s);

returning a value in a function

function hasawinner():boolean if (all the cells in the row0 is 1) else if (all the cells in the row1 is 1) else if (all the cells in the row2 is 1) else if (all the cells in the column0 is 1) else if (all the cells in the column1 is 1) else if (all the cells in the column2 is 1) else if (all the cells in the diagonal0 is 1) else if (all the cells in the diagonal1 is 1) return false;

function hasawinner():boolean if (all the cells in the row0 is 1) if (all the cells in the row1 is 1) if (all the cells in the row2 is 1) if (all the cells in the column0 is 1) if (all the cells in the column1 is 1) if (all the cells in the column2 is 1) if (all the cells in the diagonal0 is 1) if (all the cells in the diagonal1 is 1) Translated to Actionscript: markings[0][0] == 1 && markings[0][1] == 1 && markings[0][2] == 1 return false;

function hasawinner():boolean if (markings[0][0] == 1 && markings[0][1] == 1 && markings[0][2] == 1) if (all the cells in the row1 is 1) if (all the cells in the row2 is 1) if (all the cells in the column0 is 1) if (all the cells in the column1 is 1) if (all the cells in the column2 is 1) if (all the cells in the diagonal0 is 1) if (all the cells in the diagonal1 is 1) But this is only checking whether Player 1 wins What about Player 2? return false;

Solution: use variable!

function hasawinner():boolean if (markings[0][0] == currplayer && markings[0][1] == currplayer && markings[0][2] == currplayer) if (all the cells in the row1 is currplayer) if (all the cells in the row2 is currplayer) if (all the cells in the column0 is currplayer) if (all the cells in the column1 is currplayer) if (all the cells in the column2 is currplayer) if (all the cells in the diagonal0 is currplayer) if (all the cells in the diagonal1 is currplayer) return false;

if (hasawinner()) display in the dynamic textbox tb the winner is currplayer Question: But where do you place this if/else-if statement, ie how often should you call this function to check winner? A every time after a player makes a move B check it constantly at frame rate

if (hasawinner()) display in the dynamic textbox tb the winner is currplayer else swap player display in the dynamic textbox who's turn

if (hasawinner()) display in the dynamic textbox tb the winner is currplayer gameover(); else swap player display in the dynamic textbox who's turn

Step 4 code to take care of game over

When game over When there is winner, the game ends And, any unmarked cells should be nonclickable How do we code to make all cells non-clickable?

function gameover():void removeeventlistener for all the cells OK, this is just a function definition But where in the code do you call this function?

Let's review the second handout 49

Step 5 (Referred to as Stage 2 in the third handout) use of nested loop to compact the code

board[0][0]addeventlistener(mouseeventmouse_up, makemove); board[0][1]addeventlistener(mouseeventmouse_ UP, makemove); board[0][2]addeventlistener(mouseeventmouse_up, makemove); board[1][0]addeventlistener(mouseeventmouse_up, makemove); board[1][1]addeventlistener(mouseeventmouse_up, makemove); board[1][2]addeventlistener(mouseeventmouse_up, makemove); board[2][0]addeventlistener(mouseeventmouse_up, makemove); board[2][1]addeventlistener(mouseeventmouse_up, makemove); board[2][2]addeventlistener(mouseeventmouse_up, makemove); Use a nested for-loop to compact it See the gradebook example and the nested for-loop in makemove() function we have just gone over

board[0][0]removeeventlistener(mouseeventmouse_up, makemove); board[0][1]removeeventlistener(mouseeventmouse_ UP, makemove); board[0][2]removeeventlistener(mouseeventmouse_up, makemove); board[1][0]removeeventlistener(mouseeventmouse_up, makemove); board[1][1]removeeventlistener(mouseeventmouse_up, makemove); board[1][2]removeeventlistener(mouseeventmouse_up, makemove); board[2][0]removeeventlistener(mouseeventmouse_up, makemove); board[2][1]removeeventlistener(mouseeventmouse_up, makemove); board[2][2]removeeventlistener(mouseeventmouse_up, makemove); Similarly, use a nested for-loop to compact it

Extra Credits

4 Extra Credits More need to be done to make this tic-tac-toe game complete: 1 check tie and tell the player it's tie 2 use the hand cursor 3 allow restarting the game 4 use of for-loop to compact 6 if-statements in the hasawinner() function

Submitting the Extra Credits submit a separate file for each extra credit

Extra Credit #1: up to 10 points add code to check tie and tell the player it's tie Think about these questions first: How do you define tie? What are the conditions? Where should you place the code to check tie? Hint: You need to create a variable to keep the Hint: You need to create a variable to keep the count of the number of marked cells

Extra Credit #2: up to 5 points make the hand cursor appear when the mouse is over an unmarked cell regular pointer cursor if the cell has been marked Hint: use buttonmode property For example: to turn on the hand cursor for a moveclip instance: movieclipinstancebuttonmode = true; to turn off the hand cursor for a movieclip instance: movieclipinstancebuttonmode = false;

Extra Credit #3: up to 15 points add code to allow restarting the game, say upon hitting spacebar, at any time Hints: You will need to organize some of the statements t t into a function (that t serves as a reset) Such a reset function is invoked at the beginning and when you press spacebar Don't forget to make the cells to gotoandstop(1)

Extra Credit #4: up to 5 points In the hasawinner() function, use a for-loop to compact 6 (out of the 8) if-statements Hint: You can either: use two for-loops, p, each contains one if-statement or, use one for-loop, which contains two if-statements