Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Review my Swift programming assignment for a tic tac toe game/app and make the c

ID: 3707746 • Letter: R

Question

Review my Swift programming assignment for a tic tac toe game/app and make the code better. I am looking for a peer review. Make some changes to the code and explain exactly what you did and why you did it. I am looking for a peer review sort of thing. Please make sure your changes run. thank

see code below.

//

// ViewController.swift

// Tic Tac Toe CSCI 311

//

// Created by me on 3/21/18.

// Copyright © 2018 me. All rights reserved.

//

import UIKit

class ViewController: UIViewController {

  

// Keeps track of the current player

var currentPlayer = 1 // Cross player

  

// Keeps track of the 3 X 3 Tic Tac Toe Board

var state_Of_Game = [0, 0, 0, 0, 0, 0, 0, 0, 0]

  

// Displays an array of arrays containing winning combinations

let winningCombinations = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]

  

var gameIsActive = true

  

// The UI label that displays messages

@IBOutlet weak var label: UILabel!

  

// Displays different messages based on outcome of the game

@IBAction func action(_ sender: AnyObject) {

  

if (state_Of_Game[sender.tag-1] == 0 && gameIsActive == true)

{

state_Of_Game[sender.tag-1] = currentPlayer

  

if (currentPlayer == 1)

{

sender.setImage(UIImage(named:"cross2.png"), for: UIControlState())

currentPlayer = 2

}

else

{

sender.setImage(UIImage(named:"circle.png"), for: UIControlState())

currentPlayer = 1

}

}

  

   // Checks for a winning combination of moves from the players

   for combination in winningCombinations

   {

if state_Of_Game[combination[0]] != 0 && state_Of_Game[combination[0]] == state_Of_Game[combination[1]] && state_Of_Game[combination[1]] == state_Of_Game[combination[2]]

{

   gameIsActive = false

  

if state_Of_Game[combination[0]] == 1

{

   // Cross has won

label.text = "Cross has Won!"

}

else

{

// Circle has won

label.text = "Circle has Won!"

}

playAgainButton.isHidden = false

label.isHidden = false

}

}

  

// Handles condition where there is a draw and no winner is reached

gameIsActive = false

for i in state_Of_Game

{

if i == 0

{

gameIsActive = true

break

}

}

  

if gameIsActive == false

{

label.text = "Draw"

label.isHidden = false

playAgainButton.isHidden = false

  

}

}

  

@IBOutlet weak var playAgainButton: UIButton!

  

// Resets the array of all tic tac toe locations

@IBAction func playAgain(_ sender: Any) {

state_Of_Game = [0, 0, 0, 0, 0, 0, 0, 0, 0]

gameIsActive = true

currentPlayer = 1

  

playAgainButton.isHidden = true

label.isHidden = true

  

for i in 1...9

{

let button = view.viewWithTag(i) as! UIButton

button.setImage(nil, for: UIControlState())

}

  

}

  

  

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

Explanation / Answer

public class TicTacToe {

    private char[][] board;

    private char currentPlayerMark;

                                               

    public TicTacToe() {

        board = new char[3][3];

        currentPlayerMark = 'x';

        initializeBoard();

    }

               

               

    // Set/Reset the board back to all empty values.

    public void initializeBoard() {

                               

        // Loop through rows

        for (int i = 0; i < 3; i++) {

                                               

            // Loop through columns

            for (int j = 0; j < 3; j++) {

               board[i][j] = '-';

            }

        }

    }

               

               

    // Print the current board (may be replaced by GUI implementation later)

    public void printBoard() {

        System.out.println("-------------");

                               

        for (int i = 0; i < 3; i++) {

            System.out.print("| ");

            for (int j = 0; j < 3; j++) {

                System.out.print(board[i][j] + " | ");

            }

            System.out.println();

            System.out.println("-------------");

        }

    }

               

               

    // Loop through all cells of the board and if one is found to be empty (contains char '-') then return false.

    // Otherwise the board is full.

    public boolean isBoardFull() {

        boolean isFull = true;

                               

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {

                if (board[i][j] == '-') {

                    isFull = false;

                }

            }

        }

                               

        return isFull;

    }

               

               

    // Returns true if there is a win, false otherwise.

    // This calls our other win check functions to check the entire board.

    public boolean checkForWin() {

        return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin());

    }

               

               

    // Loop through rows and see if any are winners.

    private boolean checkRowsForWin() {

        for (int i = 0; i < 3; i++) {

            if (checkRowCol(board[i][0], board[i][1], board[i][2]) == true) {

                return true;

            }

        }

        return false;

    }

               

               

    // Loop through columns and see if any are winners.

    private boolean checkColumnsForWin() {

        for (int i = 0; i < 3; i++) {

            if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) {

                return true;

            }

        }

        return false;

    }

               

               

    // Check the two diagonals to see if either is a win. Return true if either wins.

    private boolean checkDiagonalsForWin() {

        return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true) || (checkRowCol(board[0][2], board[1][1], board[2][0]) == true));

    }

               

               

    // Check to see if all three values are the same (and not empty) indicating a win.

    private boolean checkRowCol(char c1, char c2, char c3) {

        return ((c1 != '-') && (c1 == c2) && (c2 == c3));

    }

               

               

    // Change player marks back and forth.

    public void changePlayer() {

        if (currentPlayerMark == 'x') {

           currentPlayerMark = 'o';

        }

        else {

            currentPlayerMark = 'x';

        }

    }

               

               

    // Places a mark at the cell specified by row and col with the mark of the current player.

    public boolean placeMark(int row, int col) {

                               

        // Make sure that row and column are in bounds of the board.

        if ((row >= 0) && (row < 3)) {

            if ((col >= 0) && (col < 3)) {

                if (board[row][col] == '-') {

                    board[row][col] = currentPlayerMark;

                    return true;

                }

            }

        }

                               

        return false;

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote