Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write a c++ code with proper use of constants for my pseudo code. I am take a class that is teaching me flow charts

Please write a c++ code with proper use of constants for my pseudo code. I am take a class that is teaching me flow charts and pseudocode and I would like to see the actual code so that I can start preparing myself for next semesters classes. Here is the pseudo code:

// main module

Module main()

// Local variables

Constant Integer ROWS = 3, COLS = 3

Declare Integer indexR, indexC

Declare winX = 0, winO = 0, player, draw

Declare String board[ROWS][COLS], LineFeed = 10

//initialize

For indexR = 0 to ROWS 1

For indexC = 0 to COLS 1

Set board[indexR][indexC] = *

End For

End For

// game

While winX == 0 and winO = 0

// enter location for X

Display enter X row:

Input player

Set indexR = player - 1

Display enter X column:

Input player

Set indexC = player 1

If board[indexR][indexC] == * Then

Set board[indexR][indexC] = X

End If

// enter location for O

Display enter O row:

Input player

Set indexR = player - 1

Display enter O column:

Input player

Set indexC = player 1

If board[indexR][indexC] == * Then

Set board[indexR][indexC] = O

End If

// check rows

For indexR = 0 to ROWS 1

If board[indexR][0] != * Then

If board[indexR][0] == board[indexR][1] Then

If board[indexR][1] == board[indexR][2] Then

If board[indexR][0] == X Then

Set winX = 1

Else

Set winO = 1

End If

End If

End If

End If

End For

// check columns

For indexC = 0 to COLS 1

If board[0][indexC] != * Then

If board[0][indexC] == board[1][indexC] Then

If board[1][indexC] == board[2][indexC] Then

If board[0][indexC] == X Then

Set winX = 1

Else

Set winO = 1

End If

End If

End If

End If

End For

// check diagonal

If board[0][0] != * Then

If board[0][0] == board[1][1] Then

If board[1][1] == board[2][2] Then

If board[0][0] == X Then

Set winX = 1

Else

Set winO = 1

End If

End If

End If

End If

If board[0][2] != * Then

If board[0][2] == board[1][1] Then

If board[1][1] == board[2][0] Then

If board[0][2] == X Then

Set winX = 1

Else

Set winO = 1

End If

End If

End If

End If

//check if draw

Set draw = 9

For indexR = 0 to ROWS 1

For indexC = 0 to COLS 1

If board[indexR][indexC] == * Then

Set draw = 0

End If

End For

End For

If draw == 9 Then

Set winX = 1

Set winO = 1

End If

//display board

For indexR = 0 to ROWS 1

For indexC = 0 to COLS 1

Display board[indexR][indexC]

End For

Display LineFeed

End For

End While

// show result

If draw == 9 Then

Display Game is a draw.

Else

If winX == 1 Then

Display X wins!

Else

Display O wins!

End If

End If

End Module

Output should look like this:

1 2 3 1 * * * 2 * * * 3 * * * Player X - enter: row column 1 1

1 2 3 1 X * * 2 * * * 3 * * * Player O - enter: row column 2 1

1 2 3 1 X * * 2 O * * 3 * * * Player X - enter: row column 1 2

1 2 3 1 X X * 2 O * * 3 * * * Player O - enter: row column 2 2

1 2 3 1 X X * 2 O O * 3 * * * Player X - enter: row column 1 3

1 2 3 1 X X X 2 O O * 3 * * * Player X is the winner ******NEW GAME******

1 2 3 1 * * * 2 * * * 3 * * * Player X - enter: row column 2 2

1 2 3 1 * * * 2 * X * 3 * * * Player O - enter: row column 1 1

1 2 3 1 O * * 2 * X * 3 * * * Player X - enter: row column 1 3

1 2 3 1 O * X 2 * X * 3 * * * Player O - enter: row column 3 1

1 2 3 1 O * X 2 * X * 3 O * * Player X - enter: row column 2 1

1 2 3 1 O * X 2 X X * 3 O * * Player O - enter: row column 2 3

1 2 3 1 O * X 2 X X O 3 O * * Player X - enter: row column 3 2

1 2 3 1 O * X 2 X X O 3 O X * Player O - enter: row column 1 2

1 2 3 1 O O X 2 X X O 3 O X * Player X - enter: row column 3 3 TIE GAME

1 2 3 1 O O X 2 X X O 3 O X X ******NEW GAME******

1 2 3 1 * * * 2 * * * 3 * * * Player X - enter: row column

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

What are quality improvement programs?

Answered: 1 week ago