In: Computer Science
Create a swap method without a temporary variable.
Implement an algorithm to figure out if someone has won a game of tic-tac-toe.
import java.util.Arrays; public class ArrayReview { private int data[]; public ArrayReview(int n) { data = new int[n]; for (int i = 0; i < n; i++) { data[i] = (int) (Math.random() * 100); } } public int[] getData() { return data; } public void setData(int[] data) { this.data = data; } @Override public String toString() { return Arrays.toString(data); } public void setKthItem(int k, int item) { if (k < 0 || k >= data.length) { return; } data[k] = item; } public int pickMaxIndex(int arr[], int start, int end) { int max = start; for (int i = start + 1; i <= end; i++) { if (arr[i] > arr[max]) { max = i; } } return max; } public void swap(int arr[], int i, int j) { if (arr[i] == arr[j]) { return; } arr[i] = arr[i] + arr[j]; arr[j] = arr[i] - arr[j]; arr[i] = arr[i] - arr[j]; } public void selectionSort() { int i, j, max_index; int n = data.length; for (i = 0; i < n - 1; i++) { max_index = i; for (j = i + 1; j < n; j++) if (data[j] > data[max_index]) max_index = j; swap(data, max_index, i); } } public static void main(String[] args) { ArrayReview array = new ArrayReview(10); System.out.println(array); System.out.println("After sorting: "); array.selectionSort(); for (int i : array.getData()) { System.out.printf("%-4d", i); } System.out.println(); } }
CODE
public void swap(int arr[], int i, int j) { if (arr[i] == arr[j]) { return; } arr[i] = arr[i] + arr[j]; arr[j] = arr[i] - arr[j]; arr[i] = arr[i] - arr[j]; }
CODE
class TicTacToe { public static final int X = 1, O = -1; public static final int EMPTY = 0; public int player = X; private int[][] board = new int[3][3]; public boolean isEmpty = false; public void putSign(int x, int y) { if(x<0 || x>2 || y<0 || y>2) { System.out.println("Invalid board position"); return; } if(board[x][y] != EMPTY) { System.out.println("Board position occupied"); return; } board[x][y] = player; player = -player; } public boolean isWin(int player) { return ((board[0][0] + board[0][1] + board[0][2] == player*3) || (board[1][0] + board[1][1] + board[1][2] == player*3) || (board[2][0] + board[2][1] + board[2][2] == player*3) || (board[0][0] + board[1][0] + board[2][0] == player*3) || (board[0][1] + board[1][1] + board[2][1] == player*3) || (board[0][2] + board[1][2] + board[2][2] == player*3) || (board[0][0] + board[1][1] + board[2][2] == player*3) || (board[2][0] + board[1][1] + board[0][2] == player*3)); } public void displayWinner() { if(isWin(X)) { System.out.println("\n X wins...!!"); isEmpty=false; } else if(isWin(O)) { System.out.println("\n O wins...!!"); isEmpty=false; } else { if(!isEmpty) { System.out.println("its a tie"); } } } public String toString() { StringBuilder s = new StringBuilder(); isEmpty = false; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { switch(board[i][j]) { case X: s.append(" X "); break; case O: s.append(" O "); break; case EMPTY: s.append(" "); isEmpty=true; break; } if(j<2) { s.append("|"); } } if(i<2) { s.append("\n-----------\n"); } } return s.toString(); } }