SHERIDAN, Wyo. — The soldiers were about to storm the fortress when they suddenly went still. James Smith, 17, and his teacher, Shirley Coulter, squinted at the desktop monitor.

James was programming his own military game, the final project in his Advanced Placement computer science principles class at Sheridan High School, here in the foothills of the Bighorn Mountains. Users competed as countries, like Israel or Japan, to take over a castle. But the game had crashed, and neither James nor Ms. Coulter — a 19-year veteran whose background is in teaching business classes — could figure out how to debug it.

“I’m learning with the kids,” she said. “They grasp it faster than I do.”

Ms. Coulter is one of hundreds of teachers in this sparsely populated state tasked with carrying out one of the most ambitious curriculum reform laws in the nation. Dozens of states have taken steps in recent years to expand students’ access to computer science, but last year, Wyoming became one of the few to require that all K-12 public schools offer it.

The mandate is part of a wide-ranging package of new laws, passed by the State Legislature last year, that is intended to wean Wyoming off its heavy reliance on the oil, gas and coal industries, and stem the flow of young people leaving for better jobs. Both major political parties have embraced the effort, as have tech companies eager to promote a national vision of rural economic revival built on coding skills.

There is little evidence that public school computer science lessons can drive economic change. But those who see them as fundamental to understanding today’s world say the grand promises from politicians do not matter. Nationwide, most students never have the opportunity to take a coding course. Now Wyoming’s 48 school districts have until the 2022-23 school year to begin teaching computer science at every grade level.

A virtual reality headset at Sheridan High School. Wyoming’s public schools must soon teach computer science at every grade level.CreditKristina Barker for The New York Times

The Wyoming Legislature did not dedicate new dollars to the plan, so schools are relying on federal funds and philanthropy.CreditKristina Barker for The New York Times

“I’m comfortable with the economic argument happening because a side effect of that is tens of thousands of fifth graders learning programming who otherwise wouldn’t have had that opportunity,” said David Weintrop, a professor at the University of Maryland and an expert on how computer science is taught.

Full of coal mines, vast cattle ranches and snow-capped peaks, Wyoming is perhaps an unlikely leader in a drive to bring coding into the classroom. Computer programming and software development account for fewer than two jobs per 1,000 here, compared with 19 per 1,000 in Washington State, according to the Bureau of Labor Statistics.

But with half of Wyoming’s revenue coming from the boom-and-bust cycles of the energy sector — one facing an uncertain futurebecause of climate change and environmental regulation — state leaders are looking to branch out.

The education mandate will not be easy to pull off. American public schools have long struggled to define computer science. Do keyboarding classes count, as they did in South Carolina until last school year? What about lessons in digital literacy, such as conducting internet research or protecting personal information online, as they do in Alabama?

Wyoming answered some of these questions with state standards released this spring. All students must learn what an algorithm is (a set of instructions a computer follows to solve a problem). They must grasp concepts such as loops (processes that repeat until certain outcomes are achieved, like entering a correct password after progressively more infuriating failures). They must study the impact of technology on society (immense and sometimes alarming). And they must learn to write their own code (window.alert(“Good luck.”);).

But low taxes are an orthodoxy in Wyoming, and the Legislature did not dedicate any new dollars to the plan. That has left schools reliant on limited state, federal and philanthropic funds — and on individual educators, like Ms. Coulter — to bear the burden of introducing an entirely new subject. It is a challenge without much precedent.

Ms. Coulter had three weeks of training in basic programming from a group called Project Lead the Way, which is backed by companies like Chevron, Toyota and Lockheed Martin. She also partnered with Anne Gunn, a computer science instructor at a local community college who visited her A.P. computer science course three days a week.

When James’s military game froze, Ms. Coulter turned to Ms. Gunn for help going through his code. The culprit: a missing parenthesis.

James’s school district in Sheridan, a city of 18,000 residents with a postcard-perfect 19th-century downtown, was lucky enough to land a $1.8 million grant from a local foundation, Whitney Benefits, to help introduce computer science.

That money helps pay for Ms. Gunn, who sees her role as teaching students and teachers simultaneously. Without deep teacher expertise, she said, students are likely to get stuck on “the cliff of confusion” and have trouble progressing from basic coding tutorials to independent programming. Several Sheridan students who seemed to have made that leap said Ms. Gunn’s help was crucial.

Kate Moran, 16, used the programming languages JavaScript and HTML to build a chemistry conversion calculator.

Jacob St. Pierre, 18, worked on a game in which players move a rocket through space. He wanted the ship’s movements to reflect dynamic gravity, and developed his own JavaScript physics library to build his world.

“In high school, you need a hobby,” he said.

Elsewhere in the district, eighth graders at Sheridan Junior High School crawled on the floor, chasing robotic birds they had programmed to waddle and chirp. Others threw electronic balls back and forth as they changed color in the air.

Both activities used block-based programming, in which students piece together pre-written bits of code with easy-to-read and customizable labels like “move Finch Left 90 degrees” or “fade from purple to green over 5 seconds.” A few students had advanced to Python, a language used in many real-world applications.

When their instructor, Chris Bloomgren, began teaching middle school computing here in 2001, computer education meant typing classes. Now Ms. Bloomgren does programming tutorials on her phone in the waiting room at the doctor’s office, just to keep a step ahead of her 13-year-old students. She estimated that she spent 40 hours per month, unpaid, training herself.

“I’m more of a facilitator than a teacher with this,” Ms. Bloomgren, 44, said.

In rural districts with less philanthropic support than Sheridan, school leaders are still debating what to do about the computer science mandate. Some plan to use video conferencing to get students help from experts far away.

George Mirich, the superintendent in ranching-focused Niobrara County, said some high school students had built and programmed drones to monitor cattle from overhead. But work like this has generally been limited to club activities and electives, he said, while a big focus of the computing curriculum has been how to safely use social media.

Mr. Mirich said that he had reassigned a staff member to elementary school computer science, and that he expected much of the new programming curriculum to be folded into traditional subjects like math.

Students work on a block coding exercise during class at Sagebrush Elementary School in Sheridan.CreditKristina Barker for The New York Times

Sharon Deutscher testing one of the virtual reality headsets at Sheridan High School.CreditKristina Barker for The New York Times

The major corporate backer of the Wyoming plan is Microsoft. Kate Behncken, vice president of Microsoft Philanthropies, had sweeping visions for the company’s nationwide push on computer science education, from closing so-called skills gaps (a concept that economists have questioned) to soothing national political tensions.

The 2016 presidential election showed “it was clear there were people across the U.S. who feel like they don’t have the same opportunities as people in the major metros,” Ms. Behncken said. “We have a responsibility to help address these issues.”

Microsoft and the state Department of Education hope to provide computer science training for at least one teacher in every Wyoming school, in part by working with the University of Wyoming and Code.org, a group backed by tech giants to promote computer science in schools. Microsoft’s presence in the state dates to 2012, when the company began work on a data center in Cheyennethatnowemploys about 100 people.

Andrew Weaver, an economist at the University of Illinois who has studied regional labor markets, said there were few success stories of promoting technology clusters in remote areas. “If anything, the growth in computing over the past 30-plus years has led to more concentration of computer jobs” in a few regions, he said.

Operations that do move to rural areas, like data centers, tend to require a less-skilled work force, Professor Weaver said. Basic IT jobs at local mines or government agencies may not require the coding skills that are the focus of the state’s push.The American CurriculumA series examining what is taught in the classroom and how it shapes the nation.Is the U.S. a Democracy? A Social Studies Battle Turns on the Nation’s ValuesApril 7, 2019

Indeed, at Sheridan High School, several of the computer science students said they assumed that if they wanted to pursue programming careers, they would need to leave their home state. Jacob, who is starting community college this fall, said he was attracted to the Seattle area. “I like bigger cities with more opportunity,” he said.

Wyoming educators say that despite the rhetoric of politicians and tech giants, they are teaching computer science to enrich their students, not to enrich the state.

“Our job is not to contain our kids in Wyoming,” said Craig Dougherty, the Sheridan superintendent. “They need to compete globally.”