Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Board {
- private final Player[][] fields;
- ...
- public Player getWinner() {
- Player winner = null;
- boolean isWon = false;
- // check columns (same x)
- for (int x = 0; x < fields.length; x++) {
- Player value = fields[x][0];
- if (value == null) {
- continue;
- }
- for (int y = 1; y < fields[x].length; y++) {
- Player current = fields[x][y];
- if (current == null || !current.equals(value)) {
- break;
- }
- if (y == fields[x].length -1) {
- isWon = true;
- winner = value;
- }
- }
- if(isWon) {
- break;
- }
- }
- if (! isWon) {
- // check rows (same y)
- for (int y = 0; y < fields[0].length; y++) {
- Player value = fields[0][y];
- if (value == null) {
- continue;
- }
- for (int x = 1; x < fields.length; x++) {
- Player current = fields[x][y];
- if (current == null || !current.equals(value)) {
- break;
- }
- if (x == fields.length -1) {
- isWon = true;
- winner = value;
- }
- }
- if(isWon) {
- break;
- }
- }
- }
- if (! isWon) {
- // check diagonal (bottom left to top right
- Player value = fields[0][0];
- if (value != null) {
- for (int i = 1; i < fields.length; i++) {
- if (fields[i][i] != value) {
- break;
- }
- if (i == fields.length -1) {
- isWon = true;
- winner = value;
- }
- }
- }
- }
- if (! isWon) {
- // check anti-diagonal (top left to bottom right)
- int length = fields.length;
- Player value = fields[0][length-1];
- if (value != null) {
- for (int i = 1; i < length; i++) {
- if (fields[i][length-i-1] != value) {
- break;
- }
- if (i == length -1) {
- isWon = true;
- winner = value;
- }
- }
- }
- }
- return winner;
- }
- ...
- }
- public enum Player {
- X("X"),
- O("O");
- private String str;
- private Player(String str) {
- this.str = str;
- }
- @Override public String toString() {
- return str;
- }
- }
- public enum Player {
- X, O;
- }
- List<Player> players = new ArrayList<>();
- for (int x = 0; x < fields.length; x++) {
- for (int y = 0; y < fields[x].length; y++) {
- players.add(fields[x][y]);
- }
- Player winner = findWinner(players);
- if (winner != null)
- return winner;
- }
- Player findWinner(List<Player> players) {
- Player win = players.get(0);
- for (Player pl : players) {
- // it's fine that we loop through index 0 again,
- // even though we've already processed it.
- // It's a fast operation and it won't change the result
- if (pl != win)
- return null;
- }
- return pl;
- }
- public Player getWinner() {
- // check columns (same x)
- for (int x = 0; x < fields.length; x++) {
- Player value = fields[x][0];
- if (value == null) {
- continue;
- }
- for (int y = 1; y < fields[x].length; y++) {
- Player current = fields[x][y];
- if (current == null || !current.equals(value)) {
- break;
- }
- if (y == fields[x].length -1) {
- // Early Return.... We have a WINNER!
- return value;
- }
- }
- }
- // there was no winner in this check
- // no need for isWon check or variable... we can't get here unless there is
- // no winner yet....
- for (int y = 0; y < fields[0].length; y++) {
- Player value = fields[0][y];
- if (value == null) {
- continue;
- }
- for (int x = 1; x < fields.length; x++) {
- Player current = fields[x][y];
- if (current == null || !current.equals(value)) {
- break;
- }
- if (x == fields.length -1) {
- // We have a winner!
- return value;
- }
- }
- }
- }
- // and so on....
- ....
- // there is no winner.
- return null;
- }
- void Main()
- {
- int[][] verticalWin = { new int[] {0,1}, new int[] {0,2} };
- int[][] horizontalWin = { new int[] {1,0}, new int[] {2,0} };
- int[][] playfield = { new int[] { 20,10,10 },
- new int[] { 20,10,20 },
- new int[] { 10,10,10}};
- var winner = GetWinner(playfield, verticalWin);
- Console.WriteLine(winner);
- winner = GetWinner(playfield, horizontalWin);
- Console.WriteLine(winner);
- }
- // Define other methods and classes here
- int GetWinner(int[][] playfield, int[][] kernel){
- for (int x=0; x<playfield[0].Length; x++){
- for (int y=0; y<playfield[0].Length; y++){
- var player = playfield[x][y];
- int k=0;
- while (k<kernel[0].Length
- && playfield.Length > (kernel[k][0] + x)
- && playfield.Length > (kernel[k][1] + y)
- && player == playfield[x+kernel[k][0]][y +kernel[k][1]]
- ) {
- k++;
- }
- if (k==kernel.Length){
- return player;
- }
- }
- }
- return 0;
- }
- int winner(int player, int pos) {
- int v = 0, h = 0, s0 = 0, s1 = 0;
- int col = pos % 3;
- int row = pos / 3;
- for (int i = 0; i < 3; i++) {
- if (frame[i][col] == player) {
- v++;
- }
- if (frame[row][i] == player) {
- h++;
- }
- }
- if (pos % 4 == 0) {
- for (int i = 0; i < 3; i++) {
- if (frame[i][i] == player) {
- s0++;
- }
- }
- }
- if ((pos + 2) % 4 == 0) {
- for (int i = 0; i < 3; i++) {
- if (frame[2 - i][i] == player) {
- s1++;
- }
- }
- }
- if (v == 3 || h == 3 || s0 == 3 || s1 == 3) {
- return player;
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement