At this moment, you are not allowed access to the question.

Available Marks: 20

The Connect-4 game board consists of 7 vertical slots or columns, numbered from 0 to 6, each of which can accommodate up to 6 tokens. Two players take turns to place a token in a slot, we're calling them Red and Yellow. The winner is the first player who has 4 of their tokens in a line, horizontally, vertically or diagonally.

Although the game can be fully solved with enough effort, a fair-to-average player can be simulated by scoring possible game positions and looking only a couple of plies ahead (a ply is an action by one player, two plies make up a move). The scoring system assigns points to each sequence of a player's tokens that have a reasonable chance of becoming a winning line. The scoring system to use for this task is as follows:

Notes:

The board is represented by 7 fixed-length strings, one for each vertical slot or column. This format best suits operations such as temporarily adding or removing a token, used in game play. Each character is a token (R or Y) or an empty position (a dot). Every legal board column thus begins with zero or more tokens, then empty symbols to make up 6 characters. You can use a different internal representation of course, but input/output uses this format.

Consider the following board:

RR....
......
YRRYR.
YYR...
RYYR..
RYY...
Y.....
It scores 37 for player R (majority in col 2, pair in col 0, pair in row 2 and a diagonal line of three producing a triple at one end and a PWP at the other). For player Y the same board scores 44 (centre col majority, pair in col 5, triple on row 1, two diagonal triples in one direction and one in the other).

Your task

Write a program that can read boards and score them for a designated player, and for full marks, can also play a full game against a player using a limited strategy. Input consists of board layouts preceded by a line containing score and either R or Y. A line containing the word end terminates the input. Output is a copy of the board in the same format (so we know you've read it properly) and the nominated player and calculated score on the next line. If you were scoring R for the above layout the output would be

RR....
......
YRRYR.
YYR...
RYYR..
RYY...
Y.....
Player R scores 37

To play the game (say after processing the normal input), use the following strategy exactly:

For example, your output for this part should look like this (previous board position shown for clarity):
R.....
Y.....
YRRY..
YYRY..
RYR...
R.....
Y.....
Move  9: scores 48-29, 34-40, 47-29, 45-29, 54-20, 37-37, 45-26 R selects column 4
Reply score 20 Y selects column 5

R.....
Y.....
YRRY..
YYRY..
RYRR..
RY....
Y.....

Test data

Test your program using the following data.

score Y
......
YRR...
YY....
YRR...
RRR...
YYY...
RRY...
score R
......
YR....
RYR...
YYRRY.
YRYYY.
RRY...
RRY...
score Y
RYRYY.
RYY...
RRRYR.
YRY...
RYRY..
R.....
RRR...
score R
RRYRYY
YRY...
YRRRYR
RYRY..
YRYRY.
......
RRR...
score Y
YRYRY.
RYRYR.
YRYRY.
YRYRYR
RRYRR.
Y.....
RY....
score R
RYRY..
YRYR..
YRR...
RYYR..
RYY...
YRYR..
RYRY..
end

Assessment

Step 0

Refresh the browser window  if the page has been idle for some time.

Step 1

Paste the output of your program for into the box below, both scoring output and game play (if attempted).

Step 2

Paste the source code for your program into the box below.

Step 3

When you are sure all the data has been entered correctly on the form press the submit button below.

You may submit multiple times. Only your most recent submission for each question will be marked.


Image source: wikimedia