TIC TAC TOE. import java.util.Scanner; import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in);

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Chapter 13 ARRAY LISTS AND ARRAYS. CHAPTER GOALS To become familiar with using array lists to collect objects To learn about common array algorithms To.
Chapter 13 ARRAY LISTS AND ARRAYS CHAPTER GOALS –To become familiar with using array lists to collect objects –To learn about common array algorithms –To.
Tic Tac Toe Game Design Using OOP
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.
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.
Integer Tic Tac Toe Let’s Begin Rules: 1.Erase all x’s and o’s from the previous game before you begin. 2.Decide which player will be x’s and which will.
Chapter 7 – Arrays.
Java Syntax Primitive data types Operators Control statements.
CSE 115 Week 12 March 31 – April 4, Announcements March 31 – Exam 8 March 31 – Exam 8 April 6 – Last day to turn in Lab 7 for a max grade of 100%,
CSE1301 Computer Programming: Lecture 27 Game Programming: Bingo.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
Chapter 13 Linked Structures - Stacks. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked implementation.
TA: Nouf Al-Harbi NoufNaief.net :::
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
CS1101X: Programming Methodology Recitation 7 Arrays II.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Chapter 5 Case Study. Chapter 5 The RPS Flowchart.
Artificial Intelligence and Robotics By Keith Bright & John DeBovis.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Introduction to Classes Chapter 6. Overview Examine the topics of classes and objects. Classes provide data encapsulation and abstraction. Class constructors.
Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
A: A: double “4” A: “34” 4.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Chomp. How is the game played Human player goes first choose a square, all to the right and down are “eaten” computer takes a turn whoever is forced to.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
CPSC 233 Tutorial Xin Liu Feb 14, Tips on keyboard input How to detect that the user just hit enter when prompted for a string import java.util.Scanner;
Chapter 9 Introduction to Arrays Fundamentals of Java.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Lecture 5 of Computer Science II
Chapter 8 – Arrays and Array Lists
Intro to Computer Science II
Arrays 1. One dimensional arrays - Review 2. Using arrays
8-Queens Puzzle.
Computer Programming Methodology Input and While Loop
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Review Operation Bingo
Conditional Loops.
Board: Objects, Arrays and Pedagogy
null, true, and false are also reserved.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Self study.
Tic Tac Toe application
CSE 143 Lecture 18 More Recursive Backtracking
Two dimensional arrays.
CSE 143 Lecture 18 More Recursive Backtracking
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Two dimensional arrays.
Principles of Object Oriented Programming
Data Structures & Programming
Presentation transcript:

TIC TAC TOE

import java.util.Scanner; import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in); TTTBoard board TTTBoard board = new TTTBoard(); System.out.println(board);board import java.util.Scanner; import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in); TTTBoard board TTTBoard board = new TTTBoard(); System.out.println(board);board

// Randomly decide who goes first Random gen = new Random(); String letter = "X"; if (gen.nextInt(2) == 1) letter = "O"; // Randomly decide who goes first Random gen = new Random(); String letter = "X"; if (gen.nextInt(2) == 1) letter = "O";

while (true){ String winner = board.getWinner();getWinner(); if (winner != null){ System.out.println(winner + "s win!"); break; } System.out.println(letter + "'s turn"); System.out.print("Enter the row[1-3]: "); int row = reader.nextInt(); System.out.print("Enter the column[1-3]: "); int column = reader.nextInt(); boolean success = board.placeXorO(letter, row, column);board.placeXorO if (success){ System.out.println(board); // Switch the player if (letter == "X") letter = "O"; else letter = "X"; } else System.out.println("Error: cell already occupied!"); } while (true){ String winner = board.getWinner();getWinner(); if (winner != null){ System.out.println(winner + "s win!"); break; } System.out.println(letter + "'s turn"); System.out.print("Enter the row[1-3]: "); int row = reader.nextInt(); System.out.print("Enter the column[1-3]: "); int column = reader.nextInt(); boolean success = board.placeXorO(letter, row, column);board.placeXorO if (success){ System.out.println(board); // Switch the player if (letter == "X") letter = "O"; else letter = "X"; } else System.out.println("Error: cell already occupied!"); }

public class TTTBoard{ private String[][] board; public TTTBoard(){ board = new String[3][3]; reset();reset } public class TTTBoard{ private String[][] board; public TTTBoard(){ board = new String[3][3]; reset();reset } CONSTRUCTOR METHOD FOR THE BOARD

toString method public String toString(){ String result = ""; for (int row = 0; row < board.length; row++){ for (int column = 0; column < board[0].length; column++) result += board[row][column] + " "; result += "\n"; } return result; } public String toString(){ String result = ""; for (int row = 0; row < board.length; row++){ for (int column = 0; column < board[0].length; column++) result += board[row][column] + " "; result += "\n"; } return result; }

getWinner method public String getWinner(){ if (winner("X"))winner return "X"; else if (winner("O")) return "O"; else return null; } public String getWinner(){ if (winner("X"))winner return "X"; else if (winner("O")) return "O"; else return null; }

Winner method (private) private boolean winner(String s){ // Create a target string for the search String test = s + s + s;test // Create an array to hold the possible strings int rows = board.length; int columns = board[0].length; String possibles[] = new String[rows + columns + 2];possibles[] // Get the three columns as strings for (int column = 0; column < columns; column ++) possibles[column] = getColumn(column, rows);getColumn // Get the three rows as strings for (int row = 0; row < rows; row ++) possibles[columns + row] = getRow(row, columns);getRow // Get the two diagonals as strings possibles[possibles.length - 2] = getDiagonal(0, rows, columns);getDiagonal( possibles[possibles.length - 1] = getDiagonal(rows - 1, rows, columns); // Search for the target string for (int i = 0; i < possibles.length; i++) if (test.equals(possibles[i])) return true; return false; } private boolean winner(String s){ // Create a target string for the search String test = s + s + s;test // Create an array to hold the possible strings int rows = board.length; int columns = board[0].length; String possibles[] = new String[rows + columns + 2];possibles[] // Get the three columns as strings for (int column = 0; column < columns; column ++) possibles[column] = getColumn(column, rows);getColumn // Get the three rows as strings for (int row = 0; row < rows; row ++) possibles[columns + row] = getRow(row, columns);getRow // Get the two diagonals as strings possibles[possibles.length - 2] = getDiagonal(0, rows, columns);getDiagonal( possibles[possibles.length - 1] = getDiagonal(rows - 1, rows, columns); // Search for the target string for (int i = 0; i < possibles.length; i++) if (test.equals(possibles[i])) return true; return false; }

getColumn() method (private) private String getColumn(int column, int rows){getColumn String result = ""; for (int row = 0; row < rows; row++) result += board[row][column]; return result; } private String getColumn(int column, int rows){getColumn String result = ""; for (int row = 0; row < rows; row++) result += board[row][column]; return result; }

getRow() method (private) private String getRow(int row, int columns){getRow String result = ""; for (int column = 0; column < columns; column++) result += board[row][column]; return result; } private String getRow(int row, int columns){getRow String result = ""; for (int column = 0; column < columns; column++) result += board[row][column]; return result; }

getDiagonal() method (private) private String getDiagonal(int startRow, int rows, int columns){ String result = ""; int column = 0; if (startRow == 0) while (startRow < rows){ result += board[startRow][column]; startRow++; column++; } else while (startRow >= 0){ result += board[startRow][column]; startRow--; column++; } return result; } private String getDiagonal(int startRow, int rows, int columns){ String result = ""; int column = 0; if (startRow == 0) while (startRow < rows){ result += board[startRow][column]; startRow++; column++; } else while (startRow >= 0){ result += board[startRow][column]; startRow--; column++; } return result; }

reset() method (public) public void reset(){ for (int row = 0; row < board.length; row++) for (int column = 0; column < board[0].length; column++) board[row][column] = "-"; } public void reset(){ for (int row = 0; row < board.length; row++) for (int column = 0; column < board[0].length; column++) board[row][column] = "-"; }

placeXorO() method (public) public boolean placeXorO(String s, int row, int column){ if (board[row - 1][column - 1].equals("-")){ board[row - 1][column - 1] = s; return true; } else return false; } public boolean placeXorO(String s, int row, int column){ if (board[row - 1][column - 1].equals("-")){ board[row - 1][column - 1] = s; return true; } else return false; }