2. Create the User Interface: Open ViewController.xib or MainStoryBoard.storyboard by double clicking it.



Similar documents
Send from your App Part 1

INTRODUCTION TO IOS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 13 02/22/2011

Learn iphone and ipad game apps development using ios 6 SDK. Beginning. ios 6 Games. Development. Lucas Jordan. ClayWare Games tm

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Praktikum Entwicklung von Mediensystemen mit

ITP 342 Mobile App Dev. Alerts

Assignment I Walkthrough

ios Development Tutorial Nikhil Yadav CSE 40816/60816: Pervasive Health 09/09/2011

Start Developing ios Apps Today

Xcode Application note

Star Micronics Cloud Services ios SDK User's Manual

Praktikum Entwicklung von Mediensystemen mit ios

How to Win at Tic-Tac-Toe

An Introduction to Modern Software Development Tools Creating A Simple GUI-Based Tool Appleʼs XCode Version 3.2.6

Development of Computer Graphics and Digital Image Processing on the iphone Luciano Fagundes

Application Programming on the Mac COSC346

ios App for Mobile Website! Documentation!

Creating a Custom Class in Xcode

Board Games. With Monkey Friends. 15 Multiplication Board Games to practice multiplication up to 10 x 10

Knappsack ios Build and Deployment Guide

MAKING MATH MORE FUN BRINGS YOU FUN MATH GAME PRINTABLES FOR HOME OR SCHOOL

How To Develop An App For Ios (Windows)

Fruit Machine. Level. Activity Checklist Follow these INSTRUCTIONS one by one. Test Your Project Click on the green flag to TEST your code

Creating Acrobat Forms Acrobat 9 Professional

Harman Developer Documentation

Assignment 2: Matchismo 2

The first step is to upload the Helicopter images from a strip. 1) Click on Resources > Create Sprite 2) Name it spr_helicopter 3) Click Edit Sprite

Copy Documents from your Computer (H Drive) to a Flash Drive

2. About iphone ios 5 Development Essentials. 5. Joining the Apple ios Developer Program

Assignment 1: Matchismo

View Controller Programming Guide for ios

Object-Oriented Design. CSE 5236: Mobile Application Development Course Coordinator: Dr. Rajiv Ramnath Instructor: Adam C.

Creating a New Search

Praktikum Entwicklung von Mediensystemen mit ios

Business Partners Mobile App User Guide

Using the Square. within

ios Dev Fest Research Network Operations Center Thursday, February 7, 13

3. How many winning lines are there in 5x5 Tic-Tac-Toe? 4. How many winning lines are there in n x n Tic-Tac-Toe?

File Management Where did it go? Teachers College Summer Workshop

Scrolling Tutorial For The Games Factory 2 / Multimedia Fusion 2

Wrightstown School District

Mobile Application Development L06: ios Drawing and Animation

A product of Byte Works, Inc. Credits Programming Mike Westerfield. Art Karen Bennett. Documentation Mike Westerfield

Triggers & Actions 10

SEMTech Solutions. Leaders in Refurbished SEMs. SEMTech Solutions Windows 7 SOFTWARE CONTROL SYSTEM

Creating Hyperlinks & Buttons InDesign CS6

Gmail: Sending, replying, attachments, and printing

Chapter 1. Introduction to ios Development. Objectives: Touch on the history of ios and the devices that support this operating system.

Introduction to Programming with Xojo

WEKA Handelsges.m.b.H. web: TRIPLE CROWN ROULETTE USER S MANUAL

Lab 2.1 Tracking Down the Bugs

Consumer Action - Fraud Bingo

Sprite Kit Programming Guide

Module 1. 4 Login-Send Message to Teacher

Math Board Games. For School or Home Education. by Teresa Evans. Copyright 2005 Teresa Evans. All rights reserved.

Mobile Apps with App Inventor

Digital Signature Certification Workflow

MEAP Edition Manning Early Access Program Hello! ios Development version 14

Outlook . User Guide IS TRAINING CENTER. 833 Chestnut St, Suite 600. Philadelphia, PA

Microsoft Outlook 2010 Hints & Tips

Note: To increase your bet by another amount, select another chip from the bottom right of the game panel.

How To Play The Math Game

How to build text and objects in the Titler

Microsoft Access 2010 handout

How to Use JCWHosting Reseller Cloud Storage Solution

Save and Restore Backups using itunes File

Objective C and iphone App

1.0-Scratch Interface 1.1. Valuable Information

ios App Development for Everyone

Creating Fill-able Forms using Acrobat 8.0: Part 1

6. If you want to enter specific formats, click the Format Tab to auto format the information that is entered into the field.

Managing your Joomla! 3 Content Management System (CMS) Website Websites For Small Business

Module One: Getting Started Opening Outlook Setting Up Outlook for the First Time Understanding the Interface...

CREATE A 3D MOVIE IN DIRECTOR

Storage and Playback Getting Started Guide

Print then Cut Calibration

DRUPAL WEB EDITING TRAINING

Everyday Math Online Games (Grades 1 to 3)

Tutorial: ios OData Application Development with REST Services. Sybase Unwired Platform 2.2 SP04

Geometer s Sketchpad. Discovering the incenter of a triangle

After 3 races the player with the highest winnings has won that day s racing.

Loteria Workshop is a great way to bring Mexican art and gaming tradition to your computer!

How to Update Bill Pay to Reflect a New Checking Account Type

ios Dev Crib Sheet In the Shadow of C

Accounting Games and Hands-on Activities. By Linda Welsh, Business Educator Whitehall High School

Using the Windows XP Backup Wizard. Introduction. Open the Backup Wizard

Outlook. Getting Started Outlook vs. Outlook Express Setting up a profile Outlook Today screen Navigation Pane

INSTRUCTION MANUAL Neo Coolcam IP Camera

Introduction to MS WINDOWS XP

Organizing and Managing

Beginning PowerPoint: Hands-On Exercise (Windows XP) Regent University

Transcription:

A Tic-Tac-Toe Example Application 1. Create a new Xcode Single View Application project. Call it something like TicTacToe or another title of your choice. Use the Storyboard support and enable Automatic Reference Counting (ARC). You can create an iphone, ipad or Universal project type. The game will work by the user touching a square on the game board that will place an X or an O depending on whose turn it is. We will check for a winner after the user places their mark on the board. If there is no winner, then we will update the text label to say that it is the next persons turn. We will also create a button for the user to reset the game. Copy the supplied images into the project. 2. Create the User Interface: Open ViewController.xib or MainStoryBoard.storyboard by double clicking it. A. Drag a UIImageView to the window and resize it to be approximately 300 x 300 and set its image to board.gif by using the Attributes Inspector. B. Drag another UIImageView to the window and resize it to 100 x 100 then check the box for User Interaction Enabled. See the following screen shot:

C. Then copy and paste that 8 more times. Move those boxes to be hovering over the spaces of the game board lining them up. The finished game board looks like this:

D. Drag a UILabel to the window and position it under the board. E. Drag a UIButton to the window and rename the text to Reset Game. The finished interface looks like this:

3. Wire the GUI components to the ViewController.h file: First, add two IBOutlets to be used for the UIImages and an NSInteger to keep track of the players as follows: IBOutlet UIImage * oimg; IBOutlet UIImage * ximg; NSInteger playertoken;

The finished code looks like this: #import <UIKit/UIKit.h> @interface ViewController : UIViewController // the X or O images IBOutlet UIImage * oimg; IBOutlet UIImage * ximg; NSInteger playertoken; @end These items will be set programmatically in the ViewContoller.m implementation so they are not wired to any of the GUI components. Next, wire the board to board and the label to whoseturn. Also wire the button to a resetbutton property. The code looks as follows: *board; @property (weak, nonatomic) IBOutlet UILabel *whoseturn; @property (weak, nonatomic) IBOutlet UIButton *resetbutton; Next, wire all 8 buttons to UIImageView IBOutlets labeled s1-s8. The finished code looks like this: *s1; *s2;

*s3; *s4; *s5; *s6; *s7; *s8; *s9; Next, add the IBAction to trigger when the touch down event on the UIButton occurs. Call the event buttonreset. The finished source code looks like this: - (IBAction)buttonReset:(UIButton *)sender; Next create three method prototypes to be used for the game logic: -(void) updateplayerinfo; -(void) resetboard; -(BOOL) checkforwin; 4. Open up the tictactoeviewcontroller.m and type the following code. Initialize the data for the game in the ViewDidLoad method as follows: -(void)viewdidload [super viewdidload]; // add the images

oimg = [UIImage imagenamed:@"o.png"]; ximg = [UIImage imagenamed:@"x.png"]; // set the player to 1 playertoken = 1; // update the label whoseturn.text =@"X goes first"; Add the method to update the player info as follows: - (void) updateplayerinfo if(playertoken == 1) playertoken = 2; whoseturn.text = @"It is O turn"; NSLog(@"playerToken = %d", playertoken); else if(playertoken == 2) playertoken = 1; whoseturn.text =@"It is X turn"; NSLog(@"playerToken = %d", playertoken); Add the action for the reset button: - (IBAction)buttonReset:(UIButton *)sender [self resetboard];

Add the implementation for the resetboard method: -(void) resetboard /// clear the images stored in the UIIMageView s1.image = NULL; s2.image = NULL; s3.image = NULL; s4.image = NULL; s5.image = NULL; s6.image = NULL; s7.image = NULL; s8.image = NULL; s9.image = NULL; // reset the player and update the label text playertoken= 1; whoseturn.text = @"X goes first"; Next, implement the game logic for the touching of the spaces on the board. // the touch event for the tic tac toe game - (void)touchesbegan:(nsset *)touches withevent:(uievent *)event UITouch *touch = [[event alltouches] anyobject]; // check to see which UIImage view was touched if(cgrectcontainspoint([s1 frame], [touch

if(playertoken==1) s1.image = ximg; if(playertoken==2) s1.image = oimg; if(cgrectcontainspoint([s2 frame], [touch if(playertoken==1) s2.image = ximg; if(playertoken==2) s2.image = oimg; if(cgrectcontainspoint([s3 frame], [touch if(playertoken==1) s3.image = ximg; if(playertoken==2) s3.image = oimg; if(cgrectcontainspoint([s4 frame], [touch if(playertoken==1) s4.image = ximg; if(playertoken==2) s4.image = oimg; if(cgrectcontainspoint([s5 frame], [touch if(playertoken==1) s5.image = ximg; if(playertoken==2) s5.image = oimg; if(cgrectcontainspoint([s6 frame], [touch

if(playertoken==1) s6.image = ximg; if(playertoken==2) s6.image = oimg; if(cgrectcontainspoint([s7 frame], [touch if(playertoken==1) s7.image = ximg; if(playertoken==2) s7.image = oimg; if(cgrectcontainspoint([s8 frame], [touch if(playertoken==1) s8.image = ximg; if(playertoken==2) s8.image = oimg; if(cgrectcontainspoint([s9 frame], [touch if(playertoken==1) s9.image = ximg; if(playertoken==2) s9.image = oimg; [self updateplayerinfo]; Now run the game and see what happens. It should work but there is no check for a winner yet. Add the following method implementation to check for a win: // method that will check to see if someone has won returns TRUE if someone wins -(BOOL) checkforwin

// HORIZONTAL WINS if((s1.image == s2.image) & (s2.image == s3.image) & (s1.image!= NULL)) if((s4.image == s5.image) & (s5.image == s6.image) & (s4.image!= NULL)) if((s7.image == s8.image) & (s8.image == s9.image) & (s7.image!= NULL)) // VERTICAL WINS if((s1.image == s4.image) & (s4.image == s7.image) & (s1.image!= NULL)) if((s2.image == s5.image) & (s5.image == s8.image) & (s2.image!= NULL)) if((s3.image == s6.image) & (s6.image == s9.image) & (s3.image!= NULL))

// DIAGONAL WINS if((s1.image == s5.image) & (s5.image == s9.image) & (s1.image!= NULL)) if((s3.image == s5.image) & (s5.image == s7.image) & (s3.image!= NULL)) return NO; You now have to check for the win someplace. You could do it in updateplayerinfo method. If so you could put something like this in that method: if ([self checkforwin]) UIAlertView *someonewon = [[UIAlertView alloc]initwithtitle:@"there's a winner!" message:@"someone Won. You have to figure out who and how you want to report it. I can't do everything for you." delegate:self cancelbuttontitle:@"ok" otherbuttontitles: nil]; [someonewon show]; [self resetboard]; Decide how you want to report the win and you have a working Tic- Tac-Toe game.

The final game looks like this: Congratulations you have a working Tic-Tac-Toe game.