Skip to editor
File
New File Ctrl+N
New Folder

Save Ctrl+S
Upload File…
Download as ZIP

Delete File

Reset Files…
View
Toggle Sidebar Ctrl+B
Toggle Terminal Ctrl+`

Word Wrap Alt+Z

Exercise Info
Run
Run Code Ctrl+Enter
Save & Run F5

Clear Output
CS173: Intro to Computer Science - Tic-Tac-Toe (3 pts)
Explorer
GitHub

Connect to GitHub to push and pull your project files to a repository.

On the page that opens, create a token with repo scope, copy it, then paste it below.

Paste the personal access token you created on GitHub with repo scope.

Click Refresh to load commit history
Exercise Info

Goals

  1. To iterate over a 2-dimensional array

Instructions

Modify the TicTacToe.java file to return whether a given array contains a winning tic-tac-toe configuration.

In this exercise [1], students will write a program that simulates a game of Tic-Tac-Toe. The Tic-Tac-Toe game is played on a 3x3 grid with two players who take turns. The first player marks moves with an X, and the second player marks moves with an O. The first player to form a horizontal, vertical, or diagonal sequence the same mark wins the game. In a 3x3 board, this is 3 such marks in a row. Your program should check that a given board configuration is a winning board.

Note that using this interface, it is necessary to compare char variables with double quotes instead of single quotes (even though you will use single quotes in real Java code). That is, to check if a variable ch is equal to the letter “X”, you would write:

if(ch == "X") { 
   // code to execute if true
}

You should use a loop in your submission. To check your iteration, a test case is included that checks a 4x4 board for tic-tac-toe using the same rules as the standard 3x3 board (that is, that the whole column, row, or diagonal is filled with the same player’s mark). In other words, you are now searching for 4 marks in a row, but should use the same logic as you had written when using the 3x3 board.

One way to go about this is to count how many X’s and O’s you find in a given row, column, or diagonal (three separate loops). If the total number of X’s or O’s is equal to the length of that row, column, or diagonal, you can return true. If, after all of these checks, you haven’t found one of these winning conditions, return false.

  1. Developed by Prof. Chris Tralie ↩


The Ursinus-WebIDE by Chris Tralie (opens in new tab) and Bill Mongan (opens in new tab)

Your browser does not support WebGL. A graphical rendering canvas would appear here.


          
No suggestions. Code quality feedback will appear here.
Not logged in java
Ln 1, Col 1