Question

C PROGRAMMING

INCLUDE MAIN FOR TESTING

WILL GIVE RATINGBoards Consider the following depiction of a tic-tac-toe board This board is represented by the 60 character C string: If we consider the Xs to be represented by 2s and the Os by 1s and the blank spaces by Os. We can encode the board above by the base-3 number 2100000003) which is equal to 15,309 Write a function that converts the C string board representation to a base-3 string void boardtob3( char b3(10], char board 60] and a function that converts the base-3 string representation back into a C string board void boardfromb3 char board[60], char b3[10]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/******************************************************************************

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>
#include<string.h>
void boardtob3(char b3[10],char board[60])
{
    int i,j;
    j=0;
    for (i=1;i<30;i++)
    {
        if (board[i*2-1]=='X'||board[i*2-1]=='x')
        {
            b3[j++] = '2';
           
        }
     if(board[i*2-1]=='O'||board[i-1]=='o')
        b3[j++] = '1';
        if(board[i*2-1]==' ')
        b3[j++] = '0';
    }
   
}
void boardfromb(char board[60],char b3[10])
{
    int i;
    //assigning every 11th character of board nextline
    for(i=1;i<5;i++)
    {
        board[i*12-1] = '\n';
    }
    for (i=1;i<=15;i++)
    {
        if((i*4-1)==11||(i*4-1)==23||(i*4-1)==35||(i*4-1)==47||(i*4-1)==59)
        {}
        else
        board[i*4-1] = '|';
    }
   
    board[59] = '\0';//last character of string is null character
    board[0] = ' ';
    if(b3[0]=='0')
    board[1] = ' ';
    else if(b3[0]=='2')
    board[1] = 'X';
   else
   board[1] = 'O';
    board[2] = ' ';
    board[4] = ' ';
     if(b3[1]=='0')
    board[5] = ' ';
    else if(b3[1]=='2')
    board[5] = 'X';
   else
   board[5] = 'O';
    board[6] = ' ';
    board[8] = ' ';
    if(b3[2]=='0')
    board[9] = ' ';
    else if(b3[2]=='2')
    board[9] = 'X';
   else
   board[9] = 'O';
   
   
    board[10] = ' ';
    for(i=12;i<15;i++)
    board[i] = '-';
    for(i=16;i<19;i++)
    board[i] = '-';
    for(i=20;i<23;i++)
    board[i] = '-';
     board[24] = ' ';
     if(b3[3]=='0')
    board[25] =' ';
    else if(b3[3]=='2')
    board[25] = 'X';
   else
   board[25] = 'O';
   
    board[26] = ' ';
    board[28] = ' ';
    if(b3[4]=='0')
    board[29] = ' ';
    else if(b3[4]=='2')
    board[29] = 'X';
   else
   board[29] = 'O';
   
    board[30] = ' ';
    board[32] = ' ';
    if(b3[5]=='0')
    board[33] = ' ';
    else if(b3[5]=='2')
    board[33] = 'X';
   else
   board[33] = 'O';
   
    board[34] = ' ';
    for(i=36;i<39;i++)
    board[i] = '-';
    for(i=40;i<43;i++)
    board[i] = '-';
    for(i=44;i<47;i++)
    board[i] = '-';
     board[48] = ' ';
     if(b3[6]=='0')
    board[49] = ' ';
    else if(b3[6]=='2')
    board[49] = 'X';
   else
   board[49] = 'O';
   
    board[50] = ' ';
    board[52] = ' ';
    if(b3[7]=='0')
    board[53] = ' ';
    else if(b3[7]=='2')
    board[53] = 'X';
   else
   board[53] = 'O';
   
    board[54] = ' ';
    board[56] = ' ';
    if(b3[8]=='0')
    board[57] =' ';
    else if(b3[8]=='2')
    board[57] = 'X';
   else
   board[57] = 'O';
   
    board[58] = ' ';
}
int main()

{ char b3[10];
int i;
    char board[60];
    printf("board\n");
    for (i=0;i<60;i++)
    scanf("%c",&board[i]);
boardtob3(b3,board);
    for(i=0;i<10;i++)
    printf("%c",b3[i]);
boardfromb(board,b3);
printf("\n");
for(i=0;i<60;i++)
printf("%c",board[i]);
    return 0;
}


Add a comment
Know the answer?
Add Answer to:
C PROGRAMMING INCLUDE MAIN FOR TESTING WILL GIVE RATING Boards Consider the following depiction of a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C programming language Purpose The purpose of this assignment is to help you to practice working...

    C programming language Purpose The purpose of this assignment is to help you to practice working with functions, arrays, and design simple algorithms Learning Outcomes ● Develop skills with multidimensional arrays ● Learn how to traverse multidimensional arrays ● Passing arrays to functions ● Develop algorithm design skills (e.g. recursion) Problem Overview Problem Overview Tic-Tac-Toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the...

  • Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you...

    Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates...

    Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates if there is a winner and who won. The board will be read in as 3x3 array of characters (x, o or . for a blank spot). You are required to write the following functions to help solve the problem: // input prompts the user for a board and inputs it // into the given array void input (char board [3][3]); // print prints...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • 1 Overview For this assignment you are required to write a Java program that plays (n,...

    1 Overview For this assignment you are required to write a Java program that plays (n, k)-tic-tac-toe; (n, k)-tic- tac-toe is played on a board of size n x n and to win the game a player needs to put k symbols on adjacent positions of the same row, column, or diagonal. The program will play against a human opponent. You will be given code for displaying the gameboard on the screen. 2 The Algorithm for Playing (n, k)-Tic-Tac-Toe The...

  • C++ Convert a Number from Binary to Decimal using a stack: The language of a computer...

    C++ Convert a Number from Binary to Decimal using a stack: The language of a computer is a sequence of Os and 1s. The numbering system we use is called the decimal system, or base 10 system. The numbering system that the computer uses is called the binary system, or base 2 system. The purpose of this exercise is to write a function to convert a string representing a binary number from base 2 to base 10. To convert a...

  • Please I need your help I have the code below but I need to add one...

    Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...

  • Please develop the following code using C programming and using the specific functions, instructi...

    Please develop the following code using C programming and using the specific functions, instructions and format given below. Again please use the functions given especially. Also don't copy any existing solution please write your own code. This is the first part of a series of two labs (Lab 7 and Lab 8) that will complete an implementation for a board-type game called Reversi (also called Othello). The goal of this lab is to write code that sets up the input...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT