A. Introduction
A method can "return" a value to the caller. Can the caller access other variables in the methods it called?
The answer is: no. This is because each variable has its scope.
B. Notes:
The scope of a variable is the section of a program in which the variable can be accessed (also called visible or in scope)
There are two types of scopes in Java
class scope, where a variable defined in a class but not in any method. Class scope variables are sometimes called global variables.
block scope, where a variable defined in a block {} of a method; it is also called a local variable
Scoping Rules:
local variables can be accessed ONLY in the enclosing block;
global variables can be accessed by all methods in the class;
formal arguments/parameters are valid ONLY within their methods
C. Try:
1. Analyze the scope of the boolean variable "win" in the main() and the checkWinner() methods below. Are they the same local variable?
2. Can you change the String variable "board" to a class scope variable and modify all methods below accordingly?
import java.util.*;
class TicTacToe {
public static void drawBoard(String board){
System.out.println("- - - - - - -");
System.out.println("| " + board.charAt(0) + " | " + board.charAt(1) + " | " + board.charAt(2) + " |");
System.out.println("- - - - - - -");
System.out.println("| " + board.charAt(3) + " | " + board.charAt(4) + " | " + board.charAt(5) + " |");
System.out.println("- - - - - - -");
System.out.println("| " + board.charAt(6) + " | " + board.charAt(7) + " | " + board.charAt(8) + " |");
System.out.println("- - - - - - -");
}
public static String updateBoard(String board, int move, char player){
return board.substring(0,move-1) + player + board.substring(move);
}
public static String checkWinner(String board){
if (board.charAt(0) == board.charAt(1) && board.charAt(1) == board.charAt(2) ||
board.charAt(3) == board.charAt(4) && board.charAt(4) == board.charAt(5) ||
board.charAt(6) == board.charAt(7) && board.charAt(7) == board.charAt(8) ||
board.charAt(1) == board.charAt(4) && board.charAt(4) == board.charAt(7) ||
board.charAt(2) == board.charAt(5) && board.charAt(5) == board.charAt(8) ||
board.charAt(0) == board.charAt(3) && board.charAt(3) == board.charAt(6) ||
board.charAt(0) == board.charAt(4) && board.charAt(4) == board.charAt(8) ||
board.charAt(2) == board.charAt(4) && board.charAt(4) == board.charAt(7) ){
boolean win = true;
return "WIN!!";
}
else {
boolean win = false;
return "";
}
}
public static String checkTie(boolean win){
if (win == false){
return "Sorry, this is a tie";
}
return "";
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean win = false;
String board = "123456789";
drawBoard(board);
for (int a = 0; a < 4; a++){
System.out.print("Player 1, enter your move (you are X): ");
int move = in.nextInt();
board = updateBoard(board, move, 'X');
drawBoard(board);
System.out.println(checkWinner(board));
if (win == true){
break;
}
System.out.println("");
System.out.print("Player 2, enter your move (you are O): ");
move = in.nextInt();
board = updateBoard(board, move, 'O');
drawBoard(board);
System.out.println(checkWinner(board));
if (win == true){
break;
}
System.out.println("");
}
System.out.print("Player 2, enter your move (you are O): ");
int move = in.nextInt();
board = updateBoard(board, move, 'O');
drawBoard(board);
System.out.println(checkWinner(board));
if (win == false){
System.out.println(checkTie(win));
}
}
}