CSC 384 Introduction to Artificial Intelligence (Jan - Apr 2023)

Assignment 3: Battleship Solitaire

 

Introducing Battleship Solitaire

In this assignment, you will write a program to solve Battleship Solitaire puzzles. This will require you to encode these puzzles as a constraint satisfaction problem (CSP) and implement a CSP solver.

Battleship Solitaire is similar to the Battleship board game. Unlike the 2-player board game, Battleship Solitaire shows the number of ship parts in each row and column, and your goal is to deduce the location of each ship.

You can play games of battleship solitaire for free at https://lukerissacher.com/battleships.

Battleship Solitaire Example

The rules of Battleship Solitaire are as follows.

  1. There are four types of ships.
    • Submarines (1x1)
    • Destroyers (1x2)
    • Cruisers (1x3)
    • Battleships (1x4)
  2. Each ship can be either horizontal or vertical, but not diagonal. 
  3. (Ship constraints) The puzzle describes the number of each type of ship.
  4. (Row constraints) The number to the left of each row describes the number of ship parts in that row.
  5. (Column constraints) The number at the top of each column describes the number of ship parts in that column.
  6. Ships cannot touch each other, not even diagonally. In other words, each ship must be surrounded by at least one square of water on all sides and corners.
  7. Some puzzles also reveal the contents of certain squares, showing whether they contain water or a ship part.
    • Where a ship part is revealed, it will indicate whether it is a middle or end portion of a ship. If the ship part is the end portion, it will show the part's orientation.
    • When a submarine (1x1) is revealed, it shows the entire ship.

 

Your Tasks

You will implement a program to solve Battleship Solitaire using backtracking search, forward checking, AC-3 arc consistency, or any other techniques you choose. 

 

Running Your Program

You will submit a file named battle.py, which contains your program that solves a Battleship Solitaire puzzle.

Your program must use python3 and run on the teach.cs server (where we run our auto-testing script).

We will test your program using several Battleship Solitaire puzzles. For each puzzle, we will run the following command. 

    python3 battle.py --inputfile <input file> --outputfile <output file>

Each command specifies one plain-text input file and one plain-text output file.

For example, if we run the following command for an input file puzzle1.txt

    python3 battle.py --inputfile puzzle1.txt --outputfile solution1.txt

The solution to puzzle1.txt will be in solution1.txt.

 

Input Format

The input file has the following format.

An example of an input file would be:

211222
140212
3210
000000
0000S0
000000
000000
00000.
000000

The above input file corresponds to the puzzle below.

Battleship Solitaire 3

 

Output format

The output contains an NxN grid representing the solution to the puzzle. Each cell has 7 possible values. There should be no '0' characters left in the output file. See the correct output for the earlier example below.

<>....
....S.
.^....
.M...S
.v.^..
...v.S

Here are examples of an input file and an output file.

 

Submission and Mark Breakdown

You should submit one file on MarkUs:

We will test your program on several puzzles of various sizes. For each puzzle, your program must terminate within 4 minutes. We strongly recommend testing your program on the teach.cs server to ensure that it terminates within the time limit.

We will provide three puzzles to you on MarkUs. Please use these to test the correctness and efficiency of your program. 

 

Starter Code

We have provided a considerable amount of starter code. We highly recommend taking lots of time to read the starter code and understand what it is doing.

Your main tasks are to:

  1. Create a CSP containing variables and constraints.
  2. Implement backtracking search and constraint propagation (forward checking or AC-3) to solve the CSP.

 

Suggestions

Formulating variables and constraints:

Backtracking and constraint propagation:

Heuristics: