Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a simple command line program that simulates the spinning of a roulette w

ID: 3552230 • Letter: C

Question

Create a simple command line program that simulates the spinning of a roulette wheel like game. The landing positions of the wheel are to be stored in a circular (the last node should point to the first and visa-versa) doubly linked list. When the program is run, generate a random number between 100 and 500 and then cycle through the wheel displaying each value stopping when the randomly generated value matches the iteration. Display the final value. Finally repeat the process with a new random number and cycle through the wheel backwards. In building the doubly linked list, simply hard code the values using Wikipedia as a reference for the color and numbers and the order of the nodes for the double linked list.

An example of running the program from the command line might be:

project04.exe

Forwards Random number: 256

B4, r21, b2, r25

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

#include<iostream>

#include<string>

using namespace std;

struct node {

int data;

char ch;

struct node *prev, *next;

};


struct node *head = NULL, *tail = NULL;


struct node *createNode(int);

void insertNode(int);

void deleteNode(int);

void display();


struct node* createNode(int data,char ch) {

struct node *ptr = (struct node *)malloc(sizeof (struct node));

ptr->data = data;

ptr->ch=ch;

ptr->prev = NULL;

ptr->next = NULL;

return (ptr);

}



/* insertion in circular linked list */

void insertNode(int data,char ch) {

struct node *temp, *ptr = createNode(data,ch);

/* list is empty */

if (!head) {

head = ptr;

head->next = head;

head->prev = head;

tail = head;

return;

} else {

/* only one node present in list */

if (head->next == head && head->prev == head) {

temp = head;

ptr->next = temp;

ptr->prev = temp->prev;

temp->prev = ptr;

temp->next = ptr;

tail = ptr;

} else {

/* do insertion next to head */

temp = head->next;

ptr->next = temp;

ptr->prev = temp->prev;

temp->prev->next = ptr;

temp->prev = ptr;

}

}

}

/* traversing the list */

void displayforward(int r) {

struct node *ptr = head;

int i = 0;

while (r--) {

cout<<ptr->ch<<ptr->data<<" ";

ptr = ptr->next;

i++;

}

cout<<"you landed on "<<(ptr->prev)->ch<<(ptr->prev)->data<<" ";

}

void displaybackward(int r) {

struct node *ptr = head;

int i = 0;

while (r--) {

cout<<ptr->ch<<ptr->data<<" ";

ptr = ptr->prev;

i++;

}

cout<<"you landed on "<<ptr->next->ch<<ptr->next->data<<" ";

}

int main() {

int i,data,r;

char ch;

// roulette wheel layout taken from wikipedia

int val[]={0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26};

for(i=0;i<37;i++)

{

if((val[i]>=1 && val[i]<=10) || (val[i]>=19 && val[i]<=28))

{

if(val[i]%2==0)

ch='b';

else ch='r';

}

else if((val[i]>=11 && val[i]<=18) || (val[i]>=29 && val[i]<=36))

{

if(val[i]%2!=0)

ch='b';

else ch='r';

}

else if(val[i]==0) ch='g';

insertNode(val[i],ch);

}

  srand(time(NULL));

r=rand()%400+100;

cout<<"Forward random number "<<r<<endl;

displayforward(r);

r=rand()%400+100;

cout<<"Backward random number "<<r<<endl;

displaybackward(r);   

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote