Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm doing a Uno Card game (in C) and I have a Circular Queue in this following order:
user -> player1 -> player2 -> player3
If any player put in the table a card that change the order of the game, would be in the opposite direction.
For example, if the user put the "reverse" card, I would have to dequeue every player and the, enqueue like that:
enqueue(player3);
enqueue(player2);
enqueue(player1);
enqueue(user);
If player1, dequeue everybody and then:
enqueue(user);
enqueue(player3);
enqueue(player2);
enqueue(player1);
If player2, dequeue everybody and then:
enqueue(player1);
enqueue(user);
enqueue(player3);
enqueue(player2);
If player3, dequeue everybody and then:
enqueue(player2);
enqueue(player1);
enqueue(user);
enqueue(player3);
It's terrible, isn't it? Put it in this way, like, one "if" to any case. I know that when someone put the "reverse" card the first thing to do is to dequeue everybody, but after that, is there a better way to enqueue following the thought above?
Could you not do this with a simple index and a step (+1 or -1)?
index = { player1, player2, player3, user }
step = { clockwise, anticlockwise}
When a reverse card is played, just change the sign of the step?
Start: index=player1, step=clockwise(+1)
Repeat
Player plays card... if reverse, step = !step
Move to next player: index += step
Until end
Obviously, index needs bounds checking etc, but you get the principal
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am developing and Django website, and I have an array of some str values(parts of url).When user is redirect to:
https://www.example.com/something/name/
how can system system knows that that is part of that arry.
I alredy have this:
urlpatterns = [
path(r'<name1>, views.example')
]
And Array:
arr = [name1,name2,name3, ....namen] # elements of array are assigned to variables in for loop.
Thank you in advance.
So you need to change urlpatterns to:
path('<str:name>,views.example)
str is data type, and other data type for this use are:
int – Matches zero or any positive integer.
str – Matches any non-empty string, excluding the path separator(‘/’).
slug – Matches any slug string, i.e. a string consisting of alphabets, digits, hyphen and under score.
uuid – Matches a UUID(universal unique identifier).
And function call to:
views.example(request, name = "name_n") # for better code reading use name_1, name_2, .. name_n instead of name1,name2 etc.
and that would be it, enjoy in Django.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
so i have this code, but when im running it, it's shows that "error : request for member 'nama' in something not a structure or union.
this is an admin program when you buy a movie ticket, and the modul need the name of the admin to print it in the ticket
struct admin
{
char nama[100];
char id[100];
char password[100];
};
struct admin pengatur[100];
ModeAdmin(pengatur[counter]);
void ModeAdmin(struct admin *c)
{
struct admin pembuka;
strcpy(pembuka.nama,c.nama);
strcpy(pembuka.id,c.id);
strcpy(pembuka.password,c.password);
printf("Welcome %s",pembuka.nama);
printf("1. Print Ticket\n");
printf("2. Add New Film\n");
}
This:
c.nama
should be:
c->nama
since c is not a struct or union, it's a pointer to a struct.
c is a pointer to a struct, not a struct itself, so you can't use the . operator on it. You need to instead use ->, which dereferences the pointer and then gets the member:
strcpy(pembuka.nama,c->nama);
strcpy(pembuka.id,c->id);
strcpy(pembuka.password,c->password);
Change
strcpy(pembuka.nama,c.nama);
to
strcpy(pembuka.nama,c->nama);
And all other occurences of c. likewise.
It is necessary because while pembuka is of type struct admin, c is only a pointer to one.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm writing a phonebook application and one of the functions is for searching the phonebook by name.
The program should print out the names the same way as the user entered them (later I will include the numbers and other info). The phonebook has 100 contacts.
sname is the name that user have entered to search.
name[a] is name list. Is there any other code like strstr? Like its operating if its found printing them if not then print("not found any") something like this:
for(a=1; a<101; a++) {
while(strstr(name[a],sname) != NULL) {
printf(strstr(name[a], sname));
}
}
You can do that: it is an infinite loop.
while(strstr(name[a],sname)!=NULL){
printf("%s\n",strstr(name[a],sname));
}
None of a, name[a], sname change in the conditions or body of the while
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create a program for my college marathon .
I want to be able start a stopwatch (timer) for every one running the marathon and as each person finishes the marathon i want to type in there id number and stop there Stopwatch (timer) and print out a statement saying ("you finished the marathon in " + (Time).
Just wondering is this possible to create and what way would i go about it .
any help would be greatly appreciated
Regards
Niall
See this answer for creating a digital clock in Swing.
Though this answer is only a clock, it could easily be made into a stop watch. Make use of the System.currentTimeMillis(). Have a static start time, that is populated to a data structure of a Runner object that will hold name startTime and endTime. For each runner when they're finished, they get their own endTime.
See this question for elapsed time formatting.
UPDATE
There are number of ways you can handle this. One solution is you could store the Runners in a HashMap
public class Runner {
long endTime;
Integer id;
public void setEndTime(long endTime) {
this.endTime = endTime;
}
}
public class GUI {
Map<Integer, Runner> runners = new HashMap<>();
public GUI {
Runner runner = new Runner(12334....) // id
map.put(runner.getId(), runner);
}
}
Like I said there are a number of ways to set the end time. One way is to have a endTime variable also in your GUI class. When you click a button, the variable will be assigned. Then in a text field you can type in the runner id, and assign the endtime to the runner in the map that matches the id. So everytime the button is pressed, it will be a new end time set the endTime variable, so each runner will get their own endTime
I try to connect CLIPS to my C program. Set of rules will be loaded from external .clp file into CLIPS. My new C program will in fixed time intervals set new facts (example (temperature 35C)) which will represent current measurements from some temperature sensors. Then expert system will be started and make some conclusions and give necessary actions based on provided measurements. Conclusions will be in the form of facts (Answer-is x).
How I can read desired fact field from CLIPS in the form of C variable ? For example, if fact of interest is (Answer-is, x), and if I know that first field is Answer-is, how can I make my C program to find that fact and read x ?
Example code which can help you to understand what I would like to do is the following:
int main(int argc,char *argv[]) {
void *theEnv;
theEnv = CreateEnvironment();
InitializeEnvironment();
Clear();
Reset();
// Load rules to CLIPS
Load("example.clp");
// Set facts and start CLIPS
AssertString("(TemperatureSensor1 35)");
AssertString("(TemperatureSensor2 32)");
AssertString("(TemperatureSensor2 41)");
Run(-1L);
// Extract expert system answer to C
// Read second field of generated fact from the example.clp
// which is in the form like (Answer-is xxx)
// ????????????????????????????
// This part I do not know ....
// ????????????????????????????
return(-1);
}
You can use the Eval API function to invoke the find-all-facts fact CLIPS query function. The return value from Eval will have the list of facts satisfying your query. You can then iterate over each fact (if there is more than one) and use the GetFactSlot function to retrieve the slot values of the fact.