Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

ELET 2300 Assignment 4 Submit your program through Blackboard – No late submissions! Make sure that you upload the right file (usually: main.cpp) !!!...

1 answer below »
ELET    2300    
Assignment    4    
Submit    your    program    through    Blackboard    –    No    late    submissions!    
Make    sure    that    you    upload    the    right    file    (usually:    main.cpp)    !!!    
    
In    this    assignment,    you    will    implement    a    simplified    game    of    Dara    in    C++    for    two    (human)    players.            
1.    First    be    sure    to    understand    how    the    game    works.    A    summary    of    the    game    rules    can    be    found    at    
the    end    of    this    document.    You    can    also    find    extra    information    at    Wikipedia:    
https:
en.wikipedia.org/wiki/Dara_(game)    and    YouTube    (https:
youtu.be/4pEY9MUYibU).    
2.    The    original    game    starts    with    the    players    taking    turns    to    place    12    pieces    each    on    a    5x6    square    
oard    (i.e.,    the    "drop"    phase).        To    simplify    the    verification    of    your    program,        the    program    will    
ead    the    initial    board    setup    from    a    file    instead    of    asking    the    players    to    enter    the    locations    of    the    
initial    pieces.    An    example    of    the    file    contents    is:    
.XOXOX
.XOOXX
O.XOOX
O.XO.O
XOX.XO
where    "X",    "O"    represent    the    tokens    of    player    1    and    2    respectively.    The    dot    (.)    represents    an    
empty    cell.    
In    your    program    the    board    will    be    represented    as    a    char    a
ay.        Do    not    use    strings.    Implement    the    
file    reading    and    board    initialization    in    a    C++    function    named    "readBoard".    
3.    Your    program    must    verify    whether    this    initial    configuration    is    a    valid    one,    i.e.,    make    sure    that    
each    player    has    exactly    12    pieces    on    the    board    without    any    3-piece    or    more    horizontal/vertical    
ow    (see    the    game    rules).        
4.    Implement    the    function    "displayBoard",    which    as    the    name    implies,    will    show    the    cu
ent    state    
of    the    game    on    the    screen.        
5.    Write    a    C++    loop    in    main(    )    to    implement    the    "move    phase"    of    the    game.    The    loop    will:    
A. Ask    player    1    for    the    location    of    the    piece    that    she    wants    to    move    and    the    movement    
direction    ('u','    d',    'r',    'l'    for    up,    down    right,    left).        If    the    piece    at    the    requested    location    
elongs    to    the    player    AND    the    target    cell    is    free,    then    execute    the    move.    Otherwise,    print    
an    e
or    and    ask    the    user    to    enter    a    different    location/move    direction.        
B. In    the    original    game,    a    valid    move    cannot    form    a    column
ow    of    4    equal    pieces    or    more    
and    cannot    put    the    latest    piece    back    to    its    previous    location.    These    two    rules    are    optional    
for    this    assignment,    but    you    can    implement    them    extra    credit.    
C. After    a    valid    move,    check    whether    a    3-same    piece    horizonal    or    vertical    row    was    formed.    
If    so,    ask    the    user    for    the    location,    i.e.,    the    row    number,    column    number,        of    the    piece    she    
wants    to    capture.        Verify    that    the    location    contains    an    opponent's    piece    and    that    is    not    
empty.    If    so,    remove    that    piece    (make    that    cell    empty).        If    not,    ask    again    the    user    for    a    
valid    location.    
D. After    each    capture,    check    whether    the    game    has    ended    (i.e.,    opponent    has    no    more    
pieces    on    the    board).        If    so,    print    a    message    announcing    so.    
E. Once    the    game    has    ended,    ask    whether    they    want    to    play    again.    If    so,    restart    the    game    by    
e-reading    the    initial    board    from    the    file    (step    2    above).    If    not,    terminate    the    program.    
Design    the    main    loop    in    modular    form    using    additional    C++    functions.    For    example,    you    can    
implement    a    "gameover"    function    that    checks    whether    all    opponent's    pieces    has    been    captured    
and    then    make    call    to    that    function    from    main(    ).    
Notes:    
• You    must    use    only    the    C++    instructions    that    we    have    covered    so    far    in    class.    
• The    program    that    you    turn    in    must    be    your    original    and    individual    work.    While    you    are    allowed    to    
discuss    the    program    assignments    in    general    terms    with    others,    you    are    not    allowed    to    share    details    of    
the    actual    solution(s)    or    program    code    with    anybody    except    the    instructor.    
• Turning    in    a    copy    of    someone    else's    program,    even    a    copy    with    extensive    changes    made    to    it,    or    
eceiving    any    unauthorized    assistance    is    a    very    serious    offense    in    this    course.    
• An    automated    plagiarism    checker    may    scan    programs    at    any    time    during    the    semester.    
• Only    submit    your    source    code    (.cpp).    This    must    be    done    on    or    before    the    due    date    through    Blackboard.    
No    late    submissions.    Blackboard    will    automatically    close    the    submission    page    right    after    the    deadline.    
• Your    program    must    include    comments    explaining    your    steps    and    must    be    properly    formatted    (see    
Program_Style_Guidelines.pdf    in    Blackboard).    
    
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗        
∗    ELET    2300    
    
∗    Programming    Assignment        

∗                    
*    Compiler    type    (if    other    then    Visual    C++)    
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗/    
    
    
    
    
    
    
*Courtesy    of    the    Mind    Research    Institute
Answered Same Day Apr 12, 2021

Solution

Arun Shankar answered on Apr 15 2021
141 Votes
input.txt
.XOXOX
XXOOX.
O..XOX
O.XOXO
XOO.XO
main
main.cpp
main.cpp
**************************
∗ Programming   Assignment  4
∗ Ariel Sifuentes 1441553
*   Compiler    type    G++ C++17
**************************
#include #include #include #include using namespace std;
void readBoard(char board[5][6])
{
  ifstream inFile;
  inFile.open("input.txt");
  if(!inFile)
  {
    cout
"\nUnable to open the file. Exiting..";
    exit(0);
  }
  char x;
  int count;
  while(inFile
x)
  {
    if(x=='.')
      x=' ';
    board[count/6][count%6] = x;
    ++count;
  }
  inFile.close();
}
void displayBoard(char board[5][6])
{
  for(int i=0;i<5;++i)
  {
    for(int j=0;j<6;++j)
      cout
"| "
oard[i][j];
    cout
"|"
endl;
  }
}
* A helper function to check if the board has
3-piece horizontally or vertically *
ool check_consecutive(char board[5][6], char c)
{
  int count=0;
  for(int i=0;i<5;++i)
  {
    count = 0;
    for(int j=0;j<6;++j)
    {
      if(board[i][j]==c)
      {
        ++count;
        if(count==3)
          return true;
      } 
      else
        count = 0;
    } 
  } 
 end of outer for loop
  
 Check vertical 
  for(int j=0;j<6;++j)
  {
    count = 0;
    for(int i=0;i<5;++i)
    {
      if(board[i][j]==c)
      {
        ++count;
        if(count==3)
          return true;
      } 
      else
        count = 0;
    }
  }
  return false;
}
ool check_valid(char board[5][6])
{
  int countX=0, countO=0;
  for(int i=0;i<5;++i)
    for(int j=0;j<6;++j)
      if(board[i][j]=='X')
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here